public override void OnApplyTemplate()
		{
			if (_popup != null)
			{
				_popup.RemoveHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopupOnPreviewMouseLeftButtonDown));
				_popup.Opened -= PopupOnOpened;
				_popup.Closed -= PopupOnClosed;
				_popup.Child = null;
			}
			if (_dropDownButton != null)
			{
				_dropDownButton.Click -= DropDownButtonOnClick;
			}
			if (_textBox != null)
			{
				_textBox.RemoveHandler(KeyDownEvent, new KeyEventHandler(TextBoxOnKeyDown));
				_textBox.RemoveHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(TextBoxOnTextChanged));
				_textBox.AddHandler(LostFocusEvent, new RoutedEventHandler(TextBoxOnLostFocus));
			}

			_textBox = GetTemplateChild(ElementTextBox) as TextBox;
			if (_textBox != null)
			{
				_textBox.AddHandler(KeyDownEvent, new KeyEventHandler(TextBoxOnKeyDown));
				_textBox.AddHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(TextBoxOnTextChanged));
				_textBox.AddHandler(LostFocusEvent, new RoutedEventHandler(TextBoxOnLostFocus));
			    _textBox.Text = Text;
			}

			_popup = GetTemplateChild(ElementPopup) as Popup;
			if (_popup != null)
			{
				_popup.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopupOnPreviewMouseLeftButtonDown));
                _popup.Opened += PopupOnOpened;
				_popup.Closed += PopupOnClosed;
                _popup.Child = _clockHostContentControl; 
				if (IsDropDownOpen)
				{
					_popup.IsOpen = true;
				}
			}

			_dropDownButton = GetTemplateChild(ElementButton) as Button;
			if (_dropDownButton != null)
			{
				_dropDownButton.Click += DropDownButtonOnClick;
            }

			base.OnApplyTemplate();
		}
예제 #2
0
        private void CompareButton_Click(object sender, RoutedEventArgs e)
        {
            if (_viewmodel.IsSingleLine)
            {
                _viewmodel.Result =
                    string.Join(
                        Environment.NewLine,
                        Compare(_viewmodel.Actual, _viewmodel.Expected)
                        );
            }
            else
            {
                ResultStackpanel.Children.Clear();  //  We have to clear the result between two comparisons.

                var compareResult = Bompare.Compare.Execute(_viewmodel.Actual, _viewmodel.Expected);
                for (var i = 0; i < compareResult.StringDifferenceList.Count; ++i)
                {
                    var cmpPair = compareResult.StringDifferenceList[i];
                    TextBox similarTextbox = null, expectedTextbox = null, actualTextbox = null;
                    if (cmpPair.Similar)
                    {
                        similarTextbox = new TextBox
                            {
                                Text = cmpPair.Text1,
                                BorderThickness = new Thickness( 5), 
                                BorderBrush = CreateColourFromHex(Properties.Resources.SimilarColour),
                                ////HACK:  This row hides the similar texts.  Do something clever about it.
                                //Visibility = Visibility.Collapsed
                            };
                    }
                    else
                    {
                        expectedTextbox = new TextBox
                            {
                                Text = cmpPair.Text1,
                                BorderThickness = new Thickness(5), 
                                BorderBrush = CreateColourFromHex(Properties.Resources.DifferentColour)
                            };
                        actualTextbox = new TextBox
                            {
                                Text = cmpPair.Text2,
                                BorderThickness = new Thickness(5), 
                                BorderBrush = CreateColourFromHex(Properties.Resources.DifferentColour)
                            };
                        expectedTextbox.AddHandler(MouseDoubleClickEvent, _notSimilarTextboxClickHandler);
                        actualTextbox.AddHandler(MouseDoubleClickEvent, _notSimilarTextboxClickHandler);
                    }

                    var stackpanel = new StackPanel {Orientation = Orientation.Vertical};
                    stackpanel.Children.Add(
                        new Label {Content = i.ToString(CultureInfo.InvariantCulture)}
                        );
                    if (null != similarTextbox)
                    {
                        stackpanel.Children.Add(similarTextbox);
                    }
                    if (null != expectedTextbox)
                    {
                        stackpanel.Children.Add(expectedTextbox);
                    }
                    if (null != actualTextbox)
                    {
                        stackpanel.Children.Add(actualTextbox);
                    }
                    ResultStackpanel.Children.Add(stackpanel);
                }
            }
        }
        /// <summary>
        /// 具体刷新Tab内容的方法
        /// </summary>
        /// <param name="tab"></param>
        private void RefreshTabContent(TabItem tab)
        {
            //logger.Debug("刷新控件ing");

            if (tab.Tag == null)
                return;

            int index = (int)tab.Tag;

            var prompts = viewModel.Prompts.Where(it => it.TabIndex == index)
                                          .ToList();

            Grid grid = new Grid();

            ScrollViewer viewer = new ScrollViewer();
            viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            viewer.Content = grid;

            tab.Content = viewer;

            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(10) });
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength() });
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(230) });
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(220) });
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(250, GridUnitType.Star) });
            StackPanel leftSP = new StackPanel();
            leftSP.Margin = new Thickness(5, 0, 30, 0);
            leftSP.SetValue(Grid.RowProperty, 1);
            leftSP.SetValue(Grid.ColumnProperty, 1);
            StackPanel middleSP = new StackPanel();
            middleSP.Margin = new Thickness(30, 0, 30, 0);
            middleSP.SetValue(Grid.RowProperty, 1);
            middleSP.SetValue(Grid.ColumnProperty, 2);
            StackPanel rightSP = new StackPanel();
            rightSP.Margin = new Thickness(30, 0, 0, 0);
            rightSP.SetValue(Grid.RowProperty, 1);
            rightSP.SetValue(Grid.ColumnProperty, 3);

            grid.Children.Add(leftSP);
            grid.Children.Add(middleSP);
            grid.Children.Add(rightSP);

            for (int i = 0; i < prompts.Count(); i++)
            {
                var prompt = prompts[i];

                if (prompt.ControlType == ControlType.TextBox
                    || prompt.ControlType == ControlType.UnEditabledTextBox
                    || prompt.ControlType == ControlType.Invisabled)
                {
                    Label label = new Label();
                    label.Foreground = getBrushById(prompt.ColorIndex);
                    label.DataContext = prompt;
                    label.SetBinding(Label.ContentProperty, new Binding("Name"));
                    label.SetBinding(Label.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    label.SetBinding(Label.ToolTipProperty, new Binding("HelpMessage"));
                    leftSP.Children.Add(label);

                    TextBox tb = new TextBox();
                    //tb.MouseDoubleClick += tb_MouseDoubleClick;
                    tb.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(tb_MouseLeftButtonDown), true);
                    tb.DataContext = prompt;
                    tb.SetBinding(TextBox.TextProperty, new Binding("PromptValue") { ValidatesOnExceptions = true, ValidatesOnDataErrors = true, NotifyOnValidationError = true });
                    tb.SetBinding(TextBox.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    tb.SetBinding(TextBox.IsEnabledProperty, new Binding("IsEnabled"));
                    tb.SetBinding(TextBox.ToolTipProperty, new Binding("HelpMessage"));
                    leftSP.Children.Add(tb);

                    TextBlock error = new TextBlock();
                    error.Foreground = Brushes.Red;
                    error.DataContext = prompt;
                    error.SetBinding(TextBlock.TextProperty, new Binding("ErrorMessage"));
                    error.SetBinding(Label.VisibilityProperty, new Binding("ErrorMessage") { Converter = new PromptValueToVisibilityConverter() });
                    leftSP.Children.Add(error);
                }
                else if (prompt.ControlType == ControlType.CheckBox)
                {
                    CheckBox cb = new CheckBox();
                    cb.Foreground = getBrushById(prompt.ColorIndex);
                    cb.Margin = new Thickness(0, 3, 0, 3);
                    cb.DataContext = prompt;
                    cb.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(tb_MouseLeftButtonDown), true);
                    cb.SetBinding(CheckBox.IsCheckedProperty, new Binding("PromptValue") { Converter = new PromptValueToCheckedConverter() });
                    cb.SetBinding(CheckBox.ContentProperty, new Binding("Name"));
                    cb.SetBinding(CheckBox.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    cb.SetBinding(CheckBox.ToolTipProperty, new Binding("HelpMessage"));
                    rightSP.Children.Add(cb);
                }
                else if (prompt.ControlType == ControlType.ComboBox)
                {
                    Label label = new Label();
                    label.Foreground = getBrushById(prompt.ColorIndex);
                    label.DataContext = prompt;
                    label.SetBinding(Label.ContentProperty, new Binding("Name"));
                    label.SetBinding(Label.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    label.SetBinding(Label.ToolTipProperty, new Binding("HelpMessage"));
                    leftSP.Children.Add(label);

                    ComboBox combo = new ComboBox();
                    combo.Foreground = getBrushById(prompt.ColorIndex);
                    combo.DataContext = prompt;
                    combo.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(tb_MouseLeftButtonDown), true);
                    combo.SetBinding(ComboBox.SelectedValueProperty, new Binding("PromptValue") { Converter = new PromptValueToIntConverter() });
                    combo.SetBinding(ComboBox.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    combo.SetBinding(ComboBox.ToolTipProperty, new Binding("HelpMessage"));
                    combo.ItemsSource = prompt.ComboBoxItemsString.Split('|');

                    //DataTemplate dt = new DataTemplate();
                    //dt.DataType = typeof(string);
                    //FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(TextBlock));

                    leftSP.Children.Add(combo);
                }
                else if (prompt.ControlType == ControlType.RadioButton)
                {
                    GroupBox gb = new GroupBox();
                    gb.Foreground = getBrushById(prompt.ColorIndex);
                    gb.DataContext = prompt;
                    gb.SetBinding(GroupBox.HeaderProperty, new Binding("Tip"));
                    gb.SetBinding(GroupBox.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    middleSP.Children.Add(gb);

                    StackPanel spinsder = new StackPanel();
                    gb.Content = spinsder;

                    RadioButton rb = new RadioButton();
                    rb.Foreground = getBrushById(prompt.ColorIndex);
                    rb.Margin = new Thickness(0, 5, 0, 5);
                    rb.DataContext = prompt;
                    rb.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(tb_MouseLeftButtonDown), true);
                    rb.SetBinding(RadioButton.ContentProperty, new Binding("Name"));
                    rb.SetBinding(RadioButton.IsCheckedProperty, new Binding("PromptValue") { Converter = new PromptValueToCheckedConverter() });
                    rb.SetBinding(RadioButton.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                    spinsder.Children.Add(rb);

                    for (int x = i + 1; x < prompts.Count; x++)
                    {
                        var nextPrompt = prompts[x];
                        if (nextPrompt.ControlType != ControlType.OtherRadioButton)
                        {
                            break;
                        }

                        RadioButton rb2 = new RadioButton();
                        rb2.Foreground = getBrushById(prompt.ColorIndex);
                        rb2.Margin = new Thickness(0, 5, 0, 5);
                        rb2.DataContext = nextPrompt;
                        rb2.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(tb_MouseLeftButtonDown), true);
                        rb2.SetBinding(RadioButton.ContentProperty, new Binding("Name"));
                        rb2.SetBinding(RadioButton.IsCheckedProperty, new Binding("PromptValue") { Converter = new PromptValueToCheckedConverter() });
                        rb2.SetBinding(RadioButton.VisibilityProperty, new Binding("Visible") { Converter = new PromptValueToVisibilityConverter() });
                        spinsder.Children.Add(rb2);

                    }
                }
            }
        }