如何使用自定义属性设置数据绑定
问题描述:
我在WPF中创建了一个简单的spinbox(numericUpDown)控件(因为没有)。如何使用自定义属性设置数据绑定
我已经创建了一个自定义的Value属性,我想用Model创建一个数据绑定。
<UserControl x:Class="PmFrameGrabber.Views.SpinBox"
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:PmFrameGrabber.Views"
mc:Ignorable="d"
d:DesignHeight="25" d:DesignWidth="100">
<UserControl.Resources>
<local:IntToStringConv x:Key="IntToStringConverter" />
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="25" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="TbValue" Grid.RowSpan="2" HorizontalContentAlignment="Right"
VerticalContentAlignment="Center" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Text="{Binding Value, Converter={StaticResource IntToStringConverter}}"/>
<Button Name="BtPlus" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" Margin="3,0,0,0"
VerticalAlignment="Center" FontSize="8" Content="+" Click="BtPlus_Click" />
<Button Name="BtMinus" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" Margin="3,0,0,0"
VerticalAlignment="Center" FontSize="8" Content="-" Click="BtMinus_Click" />
</Grid>
</UserControl>
这里是后面的代码:
public partial class SpinBox : UserControl
{
public static DependencyProperty ValueDP =
DependencyProperty.Register("Value", typeof(int), typeof(SpinBox), new UIPropertyMetadata(0));
// Public bindable properties
public int Value
{
get => (int)GetValue(ValueDP);
set => SetValue(ValueDP, value);
}
public SpinBox()
{
InitializeComponent();
DataContext = this;
}
private void BtPlus_Click(object sender, RoutedEventArgs e) => Value++;
private void BtMinus_Click(object sender, RoutedEventArgs e) => Value--;
}
另一种观点认为,我试图用这样的控制:
<local:SpinBox Width="80" Height="25" Value="{Binding Cam.ExposureTime, Mode=TwoWay}" />
在这里,我得到一个错误: WPF绑定只能设置在依赖对象的依赖属性上
模型属性是在C++/CLI写成这样:
property int ExposureTime
{
void set(int value)
{
m_settings->exposureTime = value;
OnPropertyChanged(GetPropName(Camera, ExposureTime));
}
int get()
{
return m_settings->exposureTime;
}
}
与此属性绑定适用于其他控件(文本框,标签)。
我想问题是与我的自定义SpinBox和我创建Value属性的方式。在挖掘网络的一天之后,我还没有发现还有什么要做。
答
你忽略了依赖属性命名约定,这就要求的DependencyProperty字段必须命名为<PropertyName>Property
:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(SpinBox));
public int Value
{
get => (int)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
除此之外,在用户控件构造设置
DataContext = this;
阻止你可以绑定到继承的DataContext。
有约束力的喜欢
<local:SpinBox Value="{Binding Cam.ExposureTime, Mode=TwoWay}" />
将无法工作,因为在当前的DataContext没有Cam
财产。
因此,请不要明确设置UserControl的DataContext。
相反,像这样写的“内部”绑定:
<TextBox Text="{Binding Value,
RelativeSource={RelativeSource AncestorType=UserControl}, ...}" />
非常感谢,您的点解决所有的问题。 – Safiron