控件的 WPF 上下文菜单绑定属性
WPF Context Menu Binding Properties of Control
我正在创建一个 运行 时间的文档设计器,为此我需要在 canvas 上显示所选控件的某些属性。因此,如果我右键单击标签,我需要显示字体系列、字体大小。
我想通过绑定来做到这一点,而且我确信它是这样做的,但似乎无法让代码工作(它不提供错误,但它也不起作用。让我觉得我的绑定有问题。)请看一下...
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);
}
一切正常,除了当我右键单击时我想看到大小文本框以显示当前字体值(它显示空白)。然后当我更新它的值时,我们需要更改所选控件(文本块)的字体大小。
与字体相同。我做错了什么??
首先不用担心,我大约在 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;
}
}
}
自己试一下,很喜欢,
我正在创建一个 运行 时间的文档设计器,为此我需要在 canvas 上显示所选控件的某些属性。因此,如果我右键单击标签,我需要显示字体系列、字体大小。
我想通过绑定来做到这一点,而且我确信它是这样做的,但似乎无法让代码工作(它不提供错误,但它也不起作用。让我觉得我的绑定有问题。)请看一下...
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);
}
一切正常,除了当我右键单击时我想看到大小文本框以显示当前字体值(它显示空白)。然后当我更新它的值时,我们需要更改所选控件(文本块)的字体大小。
与字体相同。我做错了什么??
首先不用担心,我大约在 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;
}
}
}
自己试一下,很喜欢,