如何将组合框的项目背景颜色绑定到网格的背景颜色?

时间:2015-03-24 09:07:19

标签: c# combobox

<Grid x:Name="BackgroundGrid" Background="Navy">

<ComboBox x:Name="BackgroundColor" Grid.Row="1" HorizontalAlignment="Right" Height="33" Width="66" VerticalAlignment="Center" Margin="0,34,10,33" PlaceholderText="" BorderThickness="0" SelectionChanged="BackgroundColor_SelectionChanged">
                <ComboBoxItem Background="Navy"/>
                <ComboBoxItem Background="Red"/>
                <ComboBoxItem Background="Yellow"/>
                <ComboBoxItem Background="Blue"/>
                <ComboBoxItem Background="Green"/>
            </ComboBox>

请注意,它在win8.1中,我已经从互联网上搜索了解决方案,但我无法在win8.1中使用它们。

1 个答案:

答案 0 :(得分:0)

我认为最可行的方法是使用ViewModel:

  • 使用TwoWay绑定到SelectedItem
  • 更新您的组合选择
  • 侦听ViewModel的SelectedItem更改的网格

这需要为你的组合框的ItemSource增加一个额外的类

public class BackgroundColorItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var localEvent = PropertyChanged;
        if (localEvent != null)
        {
            localEvent.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            if (string.Equals(title, value))
            {
                return;
            }
            title = value;
            RaisePropertyChanged("Title");
        }
    }

    private Brush brush;
    public Brush Brush
    {
        get
        {
            return brush;
        }
        set
        {
            if (Brush.Equals(brush, value))
            {
                return;
            }
            brush = value;
            RaisePropertyChanged("Brush");
        }
    }
}

然后可以在ViewModel中使用此类来填充组合框的ItemsSource,如下所示:

public class ViewModel:INotifyPropertyChanged     {         公共事件PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var localEvent = PropertyChanged;
        if (localEvent != null)
        {
            localEvent.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private readonly IList<BackgroundColorItem> items = new ObservableCollection<BackgroundColorItem>();
    public IList<BackgroundColorItem> Items
    {
        get
        {
            return items;
        }
    }

    private BackgroundColorItem selectedItem;

    public BackgroundColorItem SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            if (Object.Equals(selectedItem, value))
            {
                return;
            }
            selectedItem = value;
            RaisePropertyChanged("SelectedItem");
        }
    }

    public ViewModel()
    {
        // add the basic items to the ItemCollection during load
        Items.Add(new BackgroundColorItem { Title = "Navy", Brush = Brushes.Navy });
        Items.Add(new BackgroundColorItem { Title = "Red", Brush = Brushes.Red });
        Items.Add(new BackgroundColorItem { Title = "Yellow", Brush = Brushes.Yellow });
        Items.Add(new BackgroundColorItem { Title = "Blue", Brush = Brushes.Blue });
        Items.Add(new BackgroundColorItem { Title = "Green", Brush = Brushes.Green });
    }

在您的XAML中,您可以将ViewModel添加为资源(如有必要,为其添加新的xmlns标记(如我的xmlns:local)

然后将ComboBox的ItemsSource绑定到ViewModel.Items

<Window x:Class="Win8._1Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Win8._1Binding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:ViewModel x:Key="ScreenViewModel" />
    </Window.Resources>
    <Grid Background="{Binding SelectedItem.Brush,Source={StaticResource ScreenViewModel},Mode=OneWay}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <ComboBox ItemsSource="{Binding Items,Source={StaticResource ScreenViewModel}}" DisplayMemberPath="Title"
                  SelectedItem="{Binding SelectedItem,Source={StaticResource ScreenViewModel},Mode=TwoWay}">
            <ComboBox.Resources>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Background" Value="{Binding Brush}" />
                </Style>
            </ComboBox.Resources>
        </ComboBox>
    </Grid>
</Window>

DisplayMember Path设置ComboBox内显示的属性,ComboBox内的Style资源自动为每个ComboBoxItem配合BackgroundColorItem的Brush属性上的Background颜色绑定。

这使您的CodeBehind保持清除任何额外的代码,您可以在程序中的其他位置使用的BackgroundColorItem类,并通过更改值添加到ViewModel内的Items集合的方式,您还可以实现一些加载/保存值