2017-08-03 138 views
0

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

我插入自定义ComboBoxMainWindow这样:

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

当我手动选择的颜色,它工作正常,但如果我设置的颜色编程这样的:

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

组合框doesn`更新自己。

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

Colorpicker类,XAML部分:

<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)); 
    } 

回答

0

您可以添加回调到您的依赖项属性,发现基于新刷值的索引。你可以在下面修改你的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. 
} 
相关问题