どの依存関係プロパティとそうでないかをよりよく理解しようとしています。以下の例を作成して、ユーザーがスライダーを動かす方法に基づいてコンボボックスの選択肢を変更できるようにしました。
これを作成する際に、ViewModel プロパティで使用されるINotifyPropertyChangedと依存関係プロパティは実際には何の関係もないことを知りました。これにより、以下の例が簡略化されました。
DockPanel.Dock="Top"
しかし、次の例から、たとえば、次の種類の XAML の使用を有効にするために、次のような依存関係プロパティを再作成するにはどうすればよいでしょうか。
<local:ExtendedComboBox
Margin="5 5 5 0"
DataIdCode="{Binding ElementName=TheSource, Path=Value}">
<Image local:ExtendendedComboBox="Left" ... />
<TextBlock local:ExtendendedComboBox="Right" ... />
</local:ExtendedComboBox>
これは可能ですか?これは、以下のより単純な例と同じ種類の依存関係プロパティの使用ですか? それとも、INotifyPropertyChanged のように、WPF の別の種類のバインド テクノロジですか?
スライダー/コンボボックスの例は次のとおりです。
XAML:
<Window x:Class="TestDependency9202.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDependency9202"
Title="Window1" Height="300" Width="300">
<StackPanel>
<StackPanel
Margin="5 5 5 0"
Orientation="Horizontal">
<TextBlock Text="Customers"
Margin="0 0 3 0"/>
<Slider x:Name="TheSource"
HorizontalAlignment="Left"
Value="0"
Width="50"
SnapsToDevicePixels="True"
Minimum="0"
Margin="0 0 3 0"
Maximum="1"/>
<TextBlock Text="Employees"/>
</StackPanel>
<local:ExtendedComboBox
Margin="5 5 5 0"
DataIdCode="{Binding ElementName=TheSource, Path=Value}"/>
</StackPanel>
</Window>
コード ビハインド:
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace TestDependency9202
{
public partial class ExtendedComboBox : ComboBox
{
public static readonly DependencyProperty DataIdCodeProperty =
DependencyProperty.Register("DataIdCode", typeof(string), typeof(ExtendedComboBox),
new PropertyMetadata(string.Empty, OnDataIdCodePropertyChanged));
private static void OnDataIdCodePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
ExtendedComboBox extendedComboBox = dependencyObject as ExtendedComboBox;
extendedComboBox.OnDataIdCodePropertyChanged2(e);
}
private void OnDataIdCodePropertyChanged2(DependencyPropertyChangedEventArgs e)
{
if (DataIdCode == "0")
{
Items.Clear();
Items.Add("customer1");
Items.Add("customer2");
Items.Add("customer3");
}
else if (DataIdCode == "1")
{
Items.Clear();
Items.Add("employee1");
Items.Add("employee2");
Items.Add("employee3");
}
this.SelectedIndex = 0;
}
public string DataIdCode
{
get { return GetValue(DataIdCodeProperty).ToString(); }
set { SetValue(DataIdCodeProperty, value); }
}
public ExtendedComboBox()
{
InitializeComponent();
}
}
}