简体   繁体   English

WPF将背景色绑定到具有可观察集合的按钮

[英]WPF bind background color to a button with a observable collection

I have a question about WPF Databinding. 我对WPF数据绑定有疑问。 I want to change the background color of a button with binding values of my ObservableCollection to the button 我想通过将ObservableCollection的绑定值绑定到按钮来更改按钮的背景颜色

My Object: 我的对象:

public string Position1 { get; set; }
public string Position2 { get; set; }
public string Position3 { get; set; }
public string Position4 { get; set; }
public string Position5 { get; set; }
public string Position6 { get; set; }
public string Position7 { get; set; }

I wanted to have these positions inside of a ObservableCollection like below: 我想将这些位置放在ObservableCollection中,如下所示:

public ObservableCollection<Positions> Positions { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Positions = new ObservableCollection<Positions>();

            Positions.Add(new Positions
            {
                Position1 = "Red",
                Position2 = "Red",
                Position3 = "Red",
                Position4 = "Gray",
                Position5 = "Green",
                Position6 = "Green",
                Position7 = "Green",
            });
        }

Now I am wondering how I can bind these values to the button in XAML? 现在我想知道如何将这些值绑定到XAML中的按钮?

I have tried this: 我已经试过了:

<Button 
        DataContext="Positions[0]"
        Grid.Column="0" 
        Background="{Binding Path=Position1}" 
        x:Name="R1" 
        HorizontalAlignment="Left" 
        Height="100" 
        Margin="5,0,0,0" 
        VerticalAlignment="Top" 
        Width="109" 
        Click="R1_Click">
        <Rectangle Stroke="Black" /> 
    </Button>

I have tried to set the datacontext, but I am just very confused in how I can get the values inside of the list in XAML. 我试图设置数据上下文,但是我对如何在XAML中获取列表中的值感到非常困惑。 Does anyone know how to do this? 有谁知道如何做到这一点?

It looks like the issue is with the Binding the datacontext, since you are binding it as a string, it is not able to understand that its a property in your view model. 似乎与绑定数据上下文有关,因为您将其作为字符串进行绑定,所以无法理解它在视图模型中的属性。

Change the DataContext of your button to "DataContext="{Binding Positions[0]}"" Then decide which property you want to bind to Background . 将按钮的DataContext更改为“ DataContext =” {Binding Positions [0]}“”“,然后确定要绑定到Background的属性。

Change the code to below, 将代码更改为以下内容,

          <Button 
          DataContext="{Binding Positions[0]}"
          Grid.Column="0" 
          Background="{Binding Path=Position4}" 
          x:Name="R1" 
          HorizontalAlignment="Left" 
          Height="100" 
          Margin="5,0,0,0" 
          VerticalAlignment="Top" 
          Width="109"
          Click="R1_Click">
              <Rectangle Stroke="Black" />
          </Button>

And I hope you are doing the DataContext of your viewmodel to the window in window's constructor. 我希望您对window的构造函数中的window执行viewmodel的DataContext。

Hope this helps, if there is some other issue, then don't hesitate to come back :) 希望这会有所帮助,如果还有其他问题,请不要犹豫再回来:)

To generate multiple buttons, use an ItemsControl with ItemsSource bound to your positions collection. 要生成多个按钮,请使用ItemsControl绑定到positions集合的ItemsSource。

To actually set the background of each button you need an IConverter class to make this multi-step conversion - first from string to Color ( ColorConverter.ConvertFromString() ) and then to a SolidColorBrush using this color value. 要实际设置每个按钮的背景,您需要一个IConverter类来进行此多步骤转换-首先从string转换为ColorColorConverter.ConvertFromString() ),然后使用此颜色值转换为SolidColorBrush

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

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