简体   繁体   English

为什么我的自定义UserControl上的依赖项属性无法识别?

[英]Why is my dependency property on a custom UserControl not recognized?

I have a UserControl called BranchFilter , with the following property: 我有一个名为BranchFilterUserControl ,具有以下属性:

private int? _branchId;
public int? LocalBranchId
{
    get { return _branchId; }
    set
    {
        SetProperty(ref _branchId, value);
        OnBranchChanged();
    }
}

In the same control, I register a dependency property so that I can bind the branch id to a viewmodel: 在同一控件中,我注册了一个依赖项属性,以便可以将分支ID绑定到视图模型:

public static readonly DependencyProperty BranchIdProperty = DependencyProperty.Register(nameof(LocalBranchId), typeof(int?), typeof(BranchFilter), new UIPropertyMetadata(null));

And when I try and access this property, without even binding it, in a view, like so: 当我尝试在视图中访问此属性时,甚至没有绑定它,就像这样:

<controls:BranchFilter Grid.Row="0" BranchId="0">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="BranchChanged">
            <i:InvokeCommandAction Command="{Binding LoadItems}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</controls:BranchFilter>

I get the compile errors: 我收到编译错误:

The member "BranchId" is not recognized or is not accessible. 成员“ BranchId”无法识别或无法访问。

The property 'BranchId' was not found in type 'BranchFilter'. 在类型“ BranchFilter”中找不到属性“ BranchId”。

The property 'BranchId' does not exist in XML namespace 'clr-namespace:ApptBook.Client.Modules.Common.Controls' XML名称空间“ clr-namespace:ApptBook.Client.Modules.Common.Controls”中不存在属性“ BranchId”

I have followed every example, but they're all the same, for adding the dependency property, but everything I have tried has failed. 我已经遵循了每个示例,但是对于添加依赖项属性来说,它们都是相同的,但是我尝试过的一切都失败了。 What could be wrong with something so simple? 如此简单的事情可能有什么问题呢?

Should you not be accessing it using LocalBranchId instead of BranchId ? 您是否应该使用LocalBranchId而不是BranchId访问它?

<controls:BranchFilter Grid.Row="0" LocalBranchId="0">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="BranchChanged">
            <i:InvokeCommandAction Command="{Binding LoadItems}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</controls:BranchFilter>

I would also rename the DependencyProperty from BranchIdProperty to LocalBranchIdProperty . 我还将DependencyPropertyBranchIdProperty重命名为LocalBranchIdProperty

You should correctly declare LocalBranchId as a dependency property: 您应该正确地将LocalBranchId声明为依赖项属性:

public static readonly DependencyProperty LocalBranchIdProperty =
    DependencyProperty.Register(
        nameof(LocalBranchId), typeof(int?), typeof(BranchFilter));

public int? LocalBranchId
{
    get { return (int?)GetValue(LocalBranchIdProperty); }
    set { SetValue(LocalBranchIdProperty, value); }
}

If you need to get notified when the property value changes, you can register a PropertyChangedCallback by PropertyMetadata: 如果需要在属性值更改时得到通知,则可以通过PropertyMetadata注册PropertyChangedCallback:

public static readonly DependencyProperty LocalBranchIdProperty =
    DependencyProperty.Register(
        nameof(LocalBranchId), typeof(int?), typeof(BranchFilter),
        new PropertyMetadata(LocalBranchIdPropertyChanged));

public int? LocalBranchId
{
    get { return (int?)GetValue(LocalBranchIdProperty); }
    set { SetValue(LocalBranchIdProperty, value); }
}

private static void LocalBranchIdPropertyChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var control = (BranchFilter)obj;
    var id = (int?)e.NewValue;
    ...
}

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

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