我正在创建一个运行时文档设计器,为此我需要在画布上显示所选控件的某些属性。所以,如果我右键单击标签,我需要显示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);
}
一切工作正常,除非我右键单击我希望看到大小的文本框来显示当前字体值(它显示空白)。然后当我更新它的值时,我们需要改变所选控件(文本块)的字体大小。
与字体相同。我究竟做错了什么??
这样的WinForms,这样不好,太多不舍... – Maverik
我投下来因为你没有利用WPF,也没有以它打算使用的方式使用它。我建议学习WPF和WinForms之间的差异,学习XAML和学习MVVM。这样做会让你的生活变得更加简单。 – Kcvin
而不是让我们其他人在试图阅读时看到了什么是错的! – Maverik