如何使用自定义属性

时间:2017-10-05 09:35:08

标签: wpf binding user-controls customproperty

我在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绑定只能在dependencyobject的dependencyproperty上设置

模型属性在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属性的方式。在挖掘网络的一天后,我还没有找到其他的工作。

1 个答案:

答案 0 :(得分:1)

您忽略了依赖项属性命名约定,这要求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;
UserControl构造函数中的

阻止您可以绑定到继承的DataContext。

这样的绑定
<local:SpinBox Value="{Binding Cam.ExposureTime, Mode=TwoWay}" />

将无效,因为当前DataContext中没有Cam属性。

所以不要明确设置UserControl的DataContext。

相反,写下这样的“内部”绑定:

<TextBox Text="{Binding Value,
                RelativeSource={RelativeSource AncestorType=UserControl}, ...}" />