我是WPF的新手,我遇到了以下问题:
在我的应用程序中,我有4x4网格,其中充满了按钮,旁边是组合框。 在组合框中,我正在存储颜色,现在我想要做的是,点击按钮后,它会将颜色更改为在组合框中选择的颜色,再次单击此按钮后,颜色会更改回默认按钮背景颜色,也会在更改选定项后在组合框中我希望所有颜色都与默认按钮背景颜色不同的按钮将颜色更改为从组合框中选择的颜色。我怎样才能实现它?
以下是它应该如何显示的图像(定义形状窗口):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是ComboBox。
以下是包含按钮的网格:
<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 :
<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 :(得分:0)
如果你想要一个快速而肮脏的解决方案,你可以将'旧'颜色存储在每个按钮的Tag-Property中,大致如下:
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;
}
}