不使用XAML进行绑定[WPF] [英] Binding without XAML [WPF]

查看:88
本文介绍了不使用XAML进行绑定[WPF]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个内部带有256个按钮的应用程序,并使用for循环以C#代码将它们添加到WrapPanel中. XAML代码中未提及这些按钮. 我的问题是,当单击其中之一时,我必须使用绑定更改其颜色. 我尝试了以下代码,但是它不起作用(仅更改按钮的内容):

I am designing an application with 256 buttons inside, and I am adding them in the WrapPanel in C# code using for loop. These buttons are not mentioned in XAML code. My problem is that, when clicking on one of them, I have to change its color using binding. I tried following code, but it does not work (only changes the content of the button):

        private void NewButton_Click(object sender, RoutedEventArgs e)
    {
        Button btn = (Button)sender;

        for (int i = 0; i < counter; i++)
        {
            if (btn.Name == ("Butt" + i))
            {
                btn.Content = "works";
                MyData mydata = new MyData();
                Binding binding = new Binding("Color");
                binding.Source = mydata;
                binding.Source = btn;
                break;
            }
        }
    }

        private int counter = 0;
    public class MyData
    {
        public static Brush _Color = Brushes.Red;
        public Brush Color
        {
            get
            {
                return _Color;
            }
        }
    }
    public MainWindow()
    {

        InitializeComponent();

        int num = number(3);
        List<Button> btnList = new List<Button>();
        for(int i =0; i<(num*num); i++)
        {

            Button button = new Button();

            button.Name = "Butt" + counter;

            button.Content = "New";

            counter++;
            button.Height = 35;
            button.Width = 35;
            button.Click += new RoutedEventHandler(NewButton_Click);
            wp.Children.Add(button);

        }

推荐答案

如果您要执行的操作是将按钮的背景色绑定到"MyData"类对象,那么您就差不多了……

If what you are trying to do is bind the button's background color to your "MyData" class object, you are almost there...

首先,创建绑定对象,将源设置为新的"mydata"实例,然后将"Color"属性的路径公开.

First, create the binding object, set the source to your new instance of "mydata", and then the path to your "Color" property exposed.

然后,您需要将新的BINDING对象保存到按钮控件中,并告诉您要将BackgroundProperty绑定到新创建的绑定.以下对您的代码进行了细微调整.并不确定为什么您的方法适用于整个项目,但希望能达到您的预期.

THEN, you need to save the new BINDING object to your button control and tell it you want the BackgroundProperty bound to the newly created binding. The following minor adjustment to your code works. Not exactly why your approach is what it is for your overall project, but hopefully does what you intended.

            if (btn.Name == ("Butt" + i))
            {
                btn.Content = "works";
                MyData mydata = new MyData();
                var oBind = new Binding
                {
                    // bind its source to this view model instance
                    Source = mydata,
                    // what property on THE BUTTON do want to be bound to.
                    Path = new PropertyPath("Color")
                };

                btn.SetBinding(BackgroundProperty, oBind);
                btn.DataContext = oBind;
                break;
            }

这篇关于不使用XAML进行绑定[WPF]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆