2015-04-16 54 views
-1

我正在创建一个运行时文档设计器,为此我需要在画布上显示所选控件的某些属性。所以,如果我右键单击标签,我需要显示font-family,font-size。WPF上下文菜单绑定控件的属性

我想通过绑定来做到这一点,我确信这是它的完成方式,但似乎无法让代码工作(它没有提供错误,但它也不起作用。我觉得有什么不对我的约束力。)请看看...

TextBlock _source = (TextBlock)sender; 
      _source.Name = "txtSource"; 

      ContextMenu contxt = new ContextMenu(); 
      contxt.IsOpen = true; 

      //Font Size Menu Header 
      MenuItem menuSizeLabel = new MenuItem(); 
      menuSizeLabel.Header = "Font Size"; 
      menuSizeLabel.IsEnabled = false; 
      contxt.Items.Add(menuSizeLabel); 
      //Font Size Menu Item 
      MenuItem menuSize = new MenuItem(); 
      TextBox tbxSize = new TextBox(); 
      Binding FontSizeBinding = new Binding("FontSize"); 
      FontSizeBinding.Source = _source; 
      FontSizeBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      FontSizeBinding.Mode = BindingMode.TwoWay; 
      tbxSize.SetBinding(TextBox.FontSizeProperty, FontSizeBinding); 
      menuSize.Header = tbxSize; 
      contxt.Items.Add(menuSize); 

      //Font Size Menu Header 
      MenuItem menuFontLabel = new MenuItem(); 
      menuFontLabel.Header = "Font Family"; 
      menuFontLabel.IsEnabled = false; 
      contxt.Items.Add(menuFontLabel); 
      //Font Menu Item 
      MenuItem menuFont = new MenuItem(); 
      ComboBox cbxFont = new ComboBox(); 
      foreach (FontFamily font in Fonts.SystemFontFamilies.OrderBy(i => i.ToString())) 
      { 
       Label lbl = new Label(); 
       lbl.Content = font; 
       lbl.FontFamily = font; 
       cbxFont.Items.Add(lbl); 
      } 
      Binding FontBinding = new Binding("FontFamily"); 
      FontBinding.Source = _source; 
      FontBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      FontBinding.Mode = BindingMode.TwoWay; 
      tbxSize.SetBinding(ComboBox.FontFamilyProperty, FontBinding); 
      menuFont.Header = cbxFont; 
      contxt.Items.Add(menuFont); 
     } 

一切工作正常,除非我右键单击我希望看到大小的文本框来显示当前字体值(它显示空白)。然后当我更新它的值时,我们需要改变所选控件(文本块)的字体大小。

与字体相同。我究竟做错了什么??

+0

这样的WinForms,这样不好,太多不舍... – Maverik

+1

我投下来因为你没有利用WPF,也没有以它打算使用的方式使用它。我建议学习WPF和WinForms之间的差异,学习XAML和学习MVVM。这样做会让你的生活变得更加简单。 – Kcvin

+0

而不是让我们其他人在试图阅读时看到了什么是错的! – Maverik

回答

1

首先不要担心,我在2007年开始制作XAML时就开始制作这样的代码,但我真的很想提出这个问题。这里是你的答案

//Text 
     MenuItem menuText = new MenuItem(); 
     menuText.IsEnabled = false; 
     var textBinding = new Binding(); 
     textBinding.Source = sender; 
     textBinding.Path = new PropertyPath("Text"); 
     BindingOperations.SetBinding(menuText, MenuItem.HeaderProperty, textBinding); 
     contxt.Items.Add(menuText); 

     //Font Size Menu Header 
     MenuItem menuSizeLabel = new MenuItem(); 
     menuSizeLabel.Header = "Font Size"; 
     menuSizeLabel.IsEnabled = false; 
     contxt.Items.Add(menuSizeLabel); 

     //Font Size Menu Item 
     TextBox tbxSize = new TextBox(); 
     Binding FontSizeBinding = new Binding(); 
     FontSizeBinding.Source = sender; 
     FontSizeBinding.Path = new PropertyPath("FontSize"); 
     FontSizeBinding.Converter = new DoubleStringConverter(); 
     FontSizeBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
     FontSizeBinding.Mode = BindingMode.TwoWay; 
     BindingOperations.SetBinding(tbxSize, TextBox.TextProperty, FontSizeBinding); 
     contxt.Items.Add(tbxSize); 

     //Font Size Menu Header 
     MenuItem menuFontLabel = new MenuItem(); 
     menuFontLabel.Header = "Font Family"; 
     menuFontLabel.IsEnabled = false; 
     contxt.Items.Add(menuFontLabel); 

     //Font Menu Item 
     ComboBox cbxFont = new ComboBox(); 
     cbxFont.ItemsSource = new ObservableCollection<FontFamily>(Fonts.SystemFontFamilies.OrderBy(i => i.ToString())); 
     Binding FontBinding = new Binding("FontFamily"); 
     FontBinding.Source = sender; 
     FontBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
     FontBinding.Mode = BindingMode.TwoWay; 
     BindingOperations.SetBinding(cbxFont, ComboBox.SelectedItemProperty, FontBinding); 
     contxt.Items.Add(cbxFont); 

作为礼物转换器

public class DoubleStringConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return value.ToString(); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      try 
      { 
       return double.Parse(value.ToString()); 
      } 
      catch 
      { 
       return 12.0; 
      } 
     } 
    } 

自己试试,我真的很喜欢它,

+0

谢谢。我会检查代码,并给你相应的荣誉。 – user3524375

+0

作品!谢谢您的帮助。我意识到我绑定错误的地方。 – user3524375