绑定ComboBox(源)与按钮不能正常工作。

时间:2016-05-30 11:31:07

标签: c# wpf data-binding

您好我将16个按钮绑定到组合框,但我有问题,它只能在加载窗口时才能工作,然后当我从ComboBox中选择新颜色时,按钮的背景不会改变。

这就是我绑定的方式(Shape1Color是组合框):

  for (int i = 0; i < Shape1.Children.Count; i++)
        {
            Binding btnbinding = new Binding();
            btnbinding.Converter = new ButtonColorConverter();
            btnbinding.Source = Shape1Color.SelectedItem;
            btnbinding.NotifyOnSourceUpdated = true;
           (Shape1.Children[i] as Button).SetBinding(Button.BackgroundProperty, btnbinding);

        }

所以它只适用于窗口加载,但是当我从组合框中选择新项目时,它不会进入我的转换器,我不知道为什么。

1 个答案:

答案 0 :(得分:0)

尝试使用Shape1Color作为绑定Source并将SelectedItem设置为Path以进行绑定

Binding btnbinding = new Binding();
btnbinding.Converter = new ButtonColorConverter();
btnbinding.Source = Shape1Color;
btnbinding.Path = new PropertyPath("SelectedItem");
btnbinding.NotifyOnSourceUpdated = true;

设置btnbinding.Source = Shape1Color.SelectedItem;只能运行一次。 SelectedItem更改

时不会更改