为什么无法识别我对自定义 UserControl 的依赖性 属性?
Why is my dependency property on a custom UserControl not recognized?
我有一个名为 BranchFilter
的 UserControl
,具有以下 属性:
private int? _branchId;
public int? LocalBranchId
{
get { return _branchId; }
set
{
SetProperty(ref _branchId, value);
OnBranchChanged();
}
}
在同一个控件中,我注册了一个依赖项 属性 以便我可以将分支 ID 绑定到视图模型:
public static readonly DependencyProperty BranchIdProperty = DependencyProperty.Register(nameof(LocalBranchId), typeof(int?), typeof(BranchFilter), new UIPropertyMetadata(null));
当我尝试访问这个 属性 时,甚至没有绑定它,在一个视图中,如下所示:
<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>
我得到编译错误:
The member "BranchId" is not recognized or is not accessible.
The property 'BranchId' was not found in type 'BranchFilter'.
The property 'BranchId' does not exist in XML namespace
'clr-namespace:ApptBook.Client.Modules.Common.Controls'
我已经按照每个示例进行操作,但它们都是相同的,用于添加依赖项 属性,但我尝试的一切都失败了。这么简单的东西能有什么问题?
您不应该使用 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>
我还将 DependencyProperty
从 BranchIdProperty
重命名为 LocalBranchIdProperty
。
您应该正确地将 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); }
}
如果您需要在 属性 值更改时得到通知,您可以注册一个
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;
...
}
我有一个名为 BranchFilter
的 UserControl
,具有以下 属性:
private int? _branchId;
public int? LocalBranchId
{
get { return _branchId; }
set
{
SetProperty(ref _branchId, value);
OnBranchChanged();
}
}
在同一个控件中,我注册了一个依赖项 属性 以便我可以将分支 ID 绑定到视图模型:
public static readonly DependencyProperty BranchIdProperty = DependencyProperty.Register(nameof(LocalBranchId), typeof(int?), typeof(BranchFilter), new UIPropertyMetadata(null));
当我尝试访问这个 属性 时,甚至没有绑定它,在一个视图中,如下所示:
<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>
我得到编译错误:
The member "BranchId" is not recognized or is not accessible.
The property 'BranchId' was not found in type 'BranchFilter'.
The property 'BranchId' does not exist in XML namespace 'clr-namespace:ApptBook.Client.Modules.Common.Controls'
我已经按照每个示例进行操作,但它们都是相同的,用于添加依赖项 属性,但我尝试的一切都失败了。这么简单的东西能有什么问题?
您不应该使用 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>
我还将 DependencyProperty
从 BranchIdProperty
重命名为 LocalBranchIdProperty
。
您应该正确地将 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); }
}
如果您需要在 属性 值更改时得到通知,您可以注册一个 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;
...
}