WPF上下文菜单控件的绑定属性

时间:2015-04-16 15:43:02

标签: c# wpf xaml binding

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

我想通过绑定来做到这一点,我确信这是完成它的方式,但似乎无法使代码工作(它不会提供错误,但它不起作用或者。让我认为我的装订有问题。)请看看......

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);
        }

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

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

1 个答案:

答案 0 :(得分: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;
            }
        }
    }

亲自尝试,我真的很喜欢,