【问题标题】:BindingExpression error when binding to DependencyProperty WPF绑定到 DependencyProperty WPF 时出现 BindingExpression 错误
【发布时间】:2015-05-21 00:51:17
【问题描述】:

当我尝试从样式将 int 值绑定到自定义控件中的 DependencyProperty 时遇到问题。

MyClassVM 包含一个名为 Number 的 int。当我以相同的方式绑定时,它在标签中完美显示,但不会在我的自定义控件上设置。例如,如果我从“{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>

这就是 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;
    }
}

我得到的错误如下所示:

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')

【问题讨论】:

    标签: c# wpf binding dependency-properties


    【解决方案1】:

    不确定,但我认为您必须在绑定中提供相关来源,例如:

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

    如果您在 Control 中,请将 Window 替换为 UserControl

    【讨论】:

    • 标签有效,只是自定义控件不起作用...我在 MyClassVM 中获得了变量...但这不起作用...' ctrl:MyCustomControl.ValueProperty= "{Binding HoursRemanining, RelativeSource={RelativeSource FindAncestor, AncestorType=local:MyClassVM}}" '
    猜你喜欢
    • 2020-04-22
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    相关资源
    最近更新 更多