列表框中一次选择一个按钮(背景颜色)

时间:2012-02-29 20:28:01

标签: windows-phone-7

所以我想将当前选中的按钮设置为列表框中的绿色背景,而未选中的所有按钮应保持黑色。我怎样才能做到这一点。查看下面的示例代码..但是不能让它正常工作。

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

1 个答案:

答案 0 :(得分:2)

如果你想这样(没有Binding和转换器),你可以去: (我也假设列表框项目中只有一个按钮)

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);
    }
}
相关问题