简体   繁体   English

不使用XAML进行绑定[WPF]

[英]Binding without XAML [WPF]

I am designing an application with 256 buttons inside, and I am adding them in the WrapPanel in C# code using for loop. 我正在设计一个带有256个按钮的应用程序,并使用for循环将它们添加到WrapPanel中的C#代码中。 These buttons are not mentioned in XAML code. XAML代码中未提及这些按钮。 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;
            }
        }
    }

and

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

        }

If what you are trying to do is bind the button's background color to your "MyData" class object, you are almost there... 如果您要执行的操作是将按钮的背景色绑定到“ MyData”类对象,那么您几乎可以做到了……

First, create the binding object, set the source to your new instance of "mydata", and then the path to your "Color" property exposed. 首先,创建绑定对象,将源设置为“ mydata”的新实例,然后将“ Color”属性的路径公开。

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. 然后,您需要将新的BINDING对象保存到按钮控件中,并告诉它您希望将BackgroundProperty绑定到新创建的绑定。 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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM