2016-05-30 124 views
0

我是新來WPF和我有以下問題:Multibinding與組合框按鈕

在我的應用程序

我有一個充滿按鈕4x4網格,並組合框旁邊。 在combobox中,我正在存儲顏色,現在我想要做的是單擊按鈕後,它將顏色更改爲在組合框中選擇的顏色,再次單擊此按鈕後,顏色將更改回默認按鈕背景顏色selectedBox組合框中的selecteditem我希望所有已經具有不同於默認按鈕背景顏色的按鈕將顏色更改爲從組合框中選擇的顏色。我怎樣才能實現它?

這裏是它應該如何看起來像圖像(定義形狀窗口):example

這是我到目前爲止有:

public partial class DefineShapes : Window 
{ 
    public DefineShapes() 
    { 
     InitializeComponent(); 
     Shape1Color.ItemsSource = typeof(Colors).GetProperties(); 
     Shape1Color.SelectedValuePath = "Id"; 
     Shape1Color.SelectedValue = "{Binding SelectedId}"; 
     Shape1Color.SelectedIndex = 2; 

    } 


    private void Shape1Color_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     Color selectedColor = (Color)(Shape1Color.SelectedItem as PropertyInfo).GetValue(null, null); 
    } 
} 

Shape1Color說組合框。

下面是一個包含按鈕我網:

<Grid Name="Shape1" Grid.Row="1" Margin="10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ShowGridLines="true"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*" /> 
       <RowDefinition Height="*" /> 
       <RowDefinition Height="*" /> 
       <RowDefinition Height="*" /> 
      </Grid.RowDefinitions> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="3"></Button> 

      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="1"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="2"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="3"></Button> 

      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2" Grid.Column="1"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2" Grid.Column="2"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2" Grid.Column="3"></Button> 

      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3" Grid.Column="1"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3" Grid.Column="2"></Button> 
      <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3" Grid.Column="3"></Button> 
     </Grid> 

這裏是我的組合框

<ComboBox Name="Shape1Color" Grid.Column="1" Grid.Row="1" Height="20" Width="100" HorizontalAlignment="Center" 
       SelectionChanged="Shape1Color_SelectionChanged" VerticalAlignment="Center"> 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 

         <TextBlock Text="{Binding Path=Name}"/> 

       </DataTemplate> 
      </ComboBox.ItemTemplate> 
     </ComboBox> 

回答

0

如果你想快速和骯髒的解決方案,你可以存儲的 '老'顏色在每個按鈕的標籤屬性中,大致如下所示:

private void Shape1Color_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var senderButton = sender as Button; 
    if (senderButton.Tag is Color) { 
     senderButton.Background = (Color)senderButton.Tag; // maybe make SolidColorBrush from color instead, here 
     senderButton.Tag = null; 
    } else { 
     Color selectedColor = (Color)(Shape1Color.SelectedItem as PropertyInfo).GetValue(null, null); 
     senderButton.Tag = selectedColor; 
    } 
}