2012-02-29 3 views
2

リストボックスで現在選択されているボタンを緑色の背景に設定します。これどうやってするの。下のexamplecodeを見てください。リストボックス内で一度に1つのボタンが選択されています(背景色)

foreach(Button btn in ListBox.Items) 
btn.Background = new SolidColorBrush(Colors.Black); 
Button clickedButton = sender as Button; 
clickedButton.Background = new SolidColorBrush(Colors.Green); 
+0

(私もボタンのみがリストボックスの項目であることを仮定している)あなたのItemTemplateにを投稿したり、あなたはボタンを入力しています。 ListBoxにボタンが必要ですか? VisualStatesを使用してこの動作を行うことができます。 –

答えて

2

あなたがそれ(バインディングやコンバータなし)そのようにしたい場合は、ここであなたが行く: を

for (int i = 0; i < ListBox.Items.Count; i++) 
{ 
    Button currentButton = ListBox.Items[i] as Button; 
    if(currentButton != null) 
    { 
     if (i == ListBox.SelectedIndex) 
      currentButton.Background = new SolidColorBrush(Colors.Green); 

     else 
      currentButton.Background = new SolidColorBrush(Colors.Black); 
    } 
} 
関連する問題