Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控件的WPF上下文菜单绑定属性_C#_Wpf_Xaml_Binding - Fatal编程技术网

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

C# 控件的WPF上下文菜单绑定属性,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我正在创建一个运行时文档设计器,为此我需要在画布上显示所选控件的某些属性。所以,如果我右键点击标签,我需要显示字体系列,字体大小 我想通过绑定来实现这一点,我确信这就是它的实现方式,但似乎无法让代码正常工作(它没有提供错误,但也不起作用。这让我觉得我的绑定有问题。)请看一看 TextBlock _source = (TextBlock)sender; _source.Name = "txtSource"; ContextMenu contxt =

我正在创建一个运行时文档设计器,为此我需要在画布上显示所选控件的某些属性。所以,如果我右键点击标签,我需要显示字体系列,字体大小

我想通过绑定来实现这一点,我确信这就是它的实现方式,但似乎无法让代码正常工作(它没有提供错误,但也不起作用。这让我觉得我的绑定有问题。)请看一看

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);
        }
一切正常,除了右键单击时,我希望看到大小文本框以显示当前字体值(它显示为空白)。然后,当我更新它的值时,我们需要更改所选控件(textblock)的字体大小


字体也一样。我做错了什么?

首先不要担心,我在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);

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

那么winforms,那么糟糕,太令人沮丧了……我投了反对票,因为你没有利用WPF,也没有按照预期的方式使用它。我建议学习WPF和WinForms之间的差异、学习XAML和学习MVVM。这样做会让你的生活变得更轻松,而且不会让我们其他人在读这本书的时候眼睛都流血了,看看到底出了什么问题!你在学习,你被否决了……这太好了。我建议您将信息绑定到Xaml中的
ViewModel
,而不是代码隐藏。我正在学习,mvvm非常复杂,一旦我掌握了wpf,mvvm应该非常简单,但现在我有一个简单的问题。有时我觉得我甚至不应该问关于堆栈溢出的问题,因为正直的程序员更喜欢批评一种编码风格,而不是帮助解决我遇到的一些基本问题。谢谢。我会检查代码,并给你相应的荣誉。作品!谢谢你的帮助。我意识到我的装订哪里出了问题。
 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;
            }
        }
    }