2016-05-30 109 views
0

你好,我綁定16個按鈕組合框,但我有問題,它應該只有當窗口加載,然後當我從組合框中選擇新的顏色,按鈕的背景不會改變。綁定組合框(源)與按鈕無法正常工作。

這是我怎麼綁定(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); 

     } 

所以它僅適用窗口負載時,但後來當我從下拉框選擇新項目不會進入我的轉換器和我不知道爲什麼。

+0

爲什麼綁定將SelectedItem作爲Source而不是Path?我可能有源Shape1Color和SelectedItem路徑 – nkoniishvt

回答

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變化時它不會改變

+0

非常感謝你 – user3112193