简体   繁体   English

如何使用ComboBox SelectionChanged事件更改StackPanel颜色?

[英]How to change StackPanel color using ComboBox SelectionChanged event?

Following xaml code is okey. 遵循xaml代码是不错的。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</Window.Resources>
<Grid>
    <StackPanel Name="StackPanel1" Width="200" Height="30" Background="Red" VerticalAlignment="Top"/>
    <ComboBox Name="ComboBox1" Width="200" Height="30" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="18" Margin="0,0,0,2">
                    <Border x:Name="Border1" BorderThickness="1" CornerRadius="2" BorderBrush="Black" Width="50" VerticalAlignment="Stretch" Background="{Binding Name}"/>
                    <TextBlock Text="{Binding Name}" Margin="8,0,0,0"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>
</Window>

Following vb.net code is not okey and need to be repaired. 遵循vb.net的代码并不正确,需要修复。

Private Sub ComboBox1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
    StackPanel1.Background = Border1.Background
End Sub

Following C# code is not okey and need to be repaired. 遵循以下C#代码并不是很好,需要修复。

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    StackPanel1.Background = Border1.Background;
}

Thanks in advance 提前致谢

you can use like that. 您可以这样使用。

Change Colors to Brushes . Colors更改为“ Brushes

<Window.Resources>
    <ObjectDataProvider ObjectInstance="{x:Type Brushes}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</Window.Resources>

Add this event handler: 添加此事件处理程序:

    <ComboBox Name="ComboBox1" Width="200" Height="30" SelectionChanged="ComboBox1_OnSelectionChanged" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name">

Change the event: 更改事件:

    private readonly BrushConverter _converter = new BrushConverter();

    private void ComboBox1_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var brush = ((PropertyInfo)this.ComboBox1.SelectedItem).Name;

        this.StackPanel1.Background = (Brush)_converter.ConvertFromString(brush);
    }

You can implement your event handler like this without changing your XAML: 您可以像这样实现事件处理程序,而无需更改XAML:

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox cmb = sender as ComboBox;
    PropertyInfo pi = cmb.SelectedItem as PropertyInfo;
    if (pi != null)
    {
        Brush brush = (Brush)new BrushConverter().ConvertFromString(pi.Name);
        brush.Freeze();
        StackPanel1.Background = brush;
    }
}

The ComboBox is bound to an IEnumerable<PropertyInfo> so you cast the SelectedItem to the currently selected PropertyInfo object and then create a Brush using the name of the property ("Red, "Blue", etc.) and the BrushConverter class. ComboBox绑定到IEnumerable<PropertyInfo>因此您将SelectedItem转换为当前选定的PropertyInfo对象,然后使用属性名称(“ Red”,“ Blue”等)和BrushConverter类创建一个Brush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM