更新自定义控件

时间:2017-08-03 13:19:24

标签: c# wpf xaml combobox

我有自定义ComboBox控件,允许选择颜色,如本文Color Picker using WPF Combobox中所述。

我以ComboBox方式插入自定义MainWindow

            <local:Colorpicker  x:Name="brushesComboBox"></local:Colorpicker>

当我手动选择颜色时,它工作正常,但如果我以编程方式设置颜色:

brushesComboBox.SelectedColor= new SolidColorBrush(Colors.Aquamarine);

ComboBox不会自行更新。

据我所知,我需要修改Colorpicker类的代码,这样当设置dependancy属性时,它将强制更新内部组合框中的选定索引。我该怎么做?

Colorpicker class,xaml part:

<UserControl x:Class="WpfParabola.Colorpicker"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:local="clr-namespace:WpfParabola"
            xmlns:sys="clr-namespace:System;assembly=mscorlib" Name="uccolorpicker"
            mc:Ignorable="d"
            d:DesignHeight="20" d:DesignWidth="200">
    <UserControl.Resources>
        <ResourceDictionary>
            <ObjectDataProvider MethodName="GetType"
                ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
                <ObjectDataProvider.MethodParameters>
                    <sys:String>System.Windows.Media.Colors, PresentationCore,
                                Version=3.0.0.0, Culture=neutral,
                                PublicKeyToken=31bf3856ad364e35</sys:String>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
                MethodName="GetProperties" x:Key="colorPropertiesOdp">
            </ObjectDataProvider>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ComboBox Name="superCombo"
            ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
            SelectedValuePath="Name"
            SelectedValue="{Binding ElementName=uccolorpicker,
            Path=SelectedColor}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="50" Height="{Binding ElementName=FontSize+4}" Margin="2" Background="{Binding Name}"/>
                        <TextBlock  TextAlignment="Left" VerticalAlignment="Center" Text="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</UserControl>

Colorpicker类,代码背后:

    public partial class Colorpicker : UserControl
    {
    public Colorpicker()
    {
        InitializeComponent();
        superCombo.SelectedIndex = 0;
    }

    public Brush SelectedColor
    {
        get { return (Brush)GetValue(SelectedColorProperty); }
        set{SetValue(SelectedColorProperty, value);}
    }
    // Using a DependencyProperty as the backing store for SelectedColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedColorProperty =
        DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(Colorpicker), new UIPropertyMetadata(null));
    }

1 个答案:

答案 0 :(得分:0)

您可以向依赖项属性添加回调,并根据新的Brush值查找索引。您可以在下面修改SelectedColorProperty初始化。

public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(Colorpicker), new PropertyMetadata(default(Brush), OnSelectedColorChanged));

private static void OnSelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var newBrush = (Brush)e.NewValue;
    var colorPicker = (Colorpicker)d;
    var comboBox = colorPicker.superCombo;

    // Find your index here using the new Brush value.
    // And update the selected index of your ComboBox.
}