0
この記事で説明したColor Picker using WPF Comboboxのように、色を選択できるカスタムComboBox
コントロールがあります。カスタムコントロールの更新
私はMainWindow
にこの方法をカスタムComboBox
を挿入します。
<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));
}