简体   繁体   English

绑定到DependencyProperty WPF时,BindingExpression错误

[英]BindingExpression error when binding to DependencyProperty WPF

I have a problem when I try to bind a int value to a DependencyProperty in a custom control from a style. 当我尝试从样式的自定义控件中将int值绑定到DependencyProperty时遇到问题。

MyClassVM contains an int named Number. MyClassVM包含一个名为Number的int。 It shows perfectly in the Label, when I bind in the same way, but will not set on my custom control. 当我以相同的方式绑定时,它在Label中完美显示,但不会在自定义控件上设置。 If I change from "{Binding Number}" To "15" for example, everything works great also on the custom control. 例如,如果我从“ {Binding Number}”更改为“ 15”,则自定义控件上的所有功能也都很好。

<Style TargetType="{x:Type ItemsControl}" x:Key="TestKey">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                    <ItemsPresenter />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Style.Resources>
        <DataTemplate DataType="{x:Type local:MyClassVM}">
            <StackPanel Orientation="Horizontal">
                <StackPanel.InputBindings>
                    <MouseBinding Gesture="LeftClick" Command="{Binding DataContext.OpenProjectCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding .}" />
                </StackPanel.InputBindings>
                <ctrl:MyCustomControl Margin="5" Width="50" ctrl:MyCustomControl.ValueProperty="{Binding Number}"/>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Number}" FontSize="14" Foreground="{DynamicResource CeriseBrush}" />              
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Style.Resources>
</Style>

This is how the MyCustomControl-class looks. 这就是MyCustomControl类的外观。

public partial class MyCustomControl: CustomUserControl
{
    public int Value { get { return _value; } set { _value = value; } }

    public MyCustomControl()
    {
        InitializeComponent();
        DataContext = this;
    }

public string ValueProperty
    {
        get { return (string)GetValue(ValuePropertyProperty); }
        set { SetValue(ValuePropertyProperty, value); }
    }

    public static readonly DependencyProperty ValuePropertyProperty =
        DependencyProperty.Register("ValueProperty", typeof(int), typeof(MyCustomControl), new UIPropertyMetadata(ValuePropertyChangedHandler));


    public static void ValuePropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        ((MyCustomControl)sender).Value = (int)e.NewValue;
    }
}

The error I get looks like this: 我得到的错误如下所示:

System.Windows.Data Error: 40 : BindingExpression path error: 'Number' property not found on 'object' ''MyCustomControl' (Name='')'. BindingExpression:Path=Number; DataItem='MyCustomControl' (Name=''); target element is 'MyCustomControl' (Name=''); target property is 'ValueProperty' (type 'Int32')

Not sure, but I think you have to provide a relative source in your binding like: 不确定,但是我认为您必须在绑定中提供相对来源,例如:

Label Content="{Binding Number, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 

Replace Window with UserControl if you're in a Control 如果您在UserControl ,请用UserControl替换Window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM