如何扩展此依赖项属性示例以重新创建类似 DockPanel.Dock=“Top”的依赖项属性?
我试图更好地理解什么是依赖属性以及它们不是什么。我构建了下面的示例,该示例允许组合框的选择根据用户移动滑块的方式进行更改。
在创建这个过程中,我了解到依赖属性实际上与 ViewModel 属性中使用的 INotifyPropertyChanged 无关,这简化了下面的示例。
但是现在我如何从下面的示例重新创建 DockPanel.Dock="Top"
中看到的依赖属性类型,例如,以便我可以启用以下类型的 XAML 使用:< /strong>
<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();
}
}
}
I'm trying to get a better understanding of what dependency properties and what they are not. I've built the example below which enables a combobox's choices to change based on how the user moves a slider.
In creating this I learned that dependency properties actually have nothing to do with INotifyPropertyChanged as is used in ViewModel properties, which simplified the below example.
But now how would I go from this example below to recreating the kind of dependency property seen in DockPanel.Dock="Top"
, e.g. so I could enable the following kind of XAML use:
<local:ExtendedComboBox
Margin="5 5 5 0"
DataIdCode="{Binding ElementName=TheSource, Path=Value}">
<Image local:ExtendendedComboBox="Left" ... />
<TextBlock local:ExtendendedComboBox="Right" ... />
</local:ExtendedComboBox>
Is this possible? And is this the same kind of use of dependency properties as in the more straight-forward example below, or is this, like INotifyPropertyChanged, yet another kind of binding technology in WPF?
Here is the slider/combobox example:
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>
Code-Behind:
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种依赖属性称为附加属性。这基本上是在另一个对象上使用的依赖属性。您使用 DependencyProperty.RegisterAttached 来创建它们,并提供两个用于获取和设置它们的静态方法。请参阅上面的链接。
This kind of dependency property is called Attached Property. That's basically a dependency property that is used on another object. You're using
DependencyProperty.RegisterAttached
to create them, and provide two static methods for getting and setting them. See the above link.