使用自定义依赖项的数据绑定 - 属性 的 UserControl 不起作用
Data-Binding with Custom Dependency-Property of UserControl is not working
我有一个带有 2 个自定义 DependencyProperty
的 UserControl
(ColumnsCount
、RowsCount
):
public partial class CabinetGrid : UserControl
{
public static readonly DependencyProperty ColumnsCountProperty =
DependencyProperty.Register("ColumnsCount", typeof (int), typeof (CabinetGrid));
public static readonly DependencyProperty RowsCountProperty =
DependencyProperty.Register("RowsCount", typeof (int), typeof (CabinetGrid));
public int ColumnsCount
{
get { return (int) GetValue(ColumnsCountProperty); }
set { SetValue(ColumnsCountProperty, value); }
}
public int RowsCount
{
get { return (int) GetValue(RowsCountProperty); }
set { SetValue(RowsCountProperty, value); }
}
}
这是 DataBinding
:
<view:CabinetGrid Grid.Column="1" Grid.Row="2" x:Name="GridRack" ColumnsCount="{Binding SelectedRoom.ColumnCount}" />
而 window 的 DataContext
有一个 属性 SelectedRoom
调用 PropertyChanged-Event
.
通过调试,我知道 UserControl
的 DataContext
设置正确。
但是,当 SelectedRoom
发生变化时(=> 我在列表中选择了另一个项目),我的 UserControl
的 DependencyProperty
ColumnsCount
不会更新。
我很沮丧,因为我已经花了一整天的时间调试这个意想不到的狗屎,使用像 XAMLSpy
和 WpfSpoon
.
这样的工具
请帮忙。
编辑:
Clemens 已经指出,CLR-Property
中包含 DependencyProperty
(ColumnsCount
) 的断点 未触发 。这是一个主要问题,因为我必须在更改时调用一些方法。我正在尝试使用 PropertyChangedCallback
,但目前遇到了一些错误。
为了获得有关依赖项 属性 的值更改的通知,您应该在注册 属性.
时在 PropertyMetadata 中指定 PropertyChangedCallback
public static readonly DependencyProperty ColumnsCountProperty =
DependencyProperty.Register(
"ColumnsCount", typeof(int), typeof(CabinetGrid),
new PropertyMetadata(OnColumnsCountPropertyChanged));
private static void OnColumnsCountPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var cabinetGrid = (CabinetGrid)obj;
// do something with the CabinetGrid instance
}
我有一个带有 2 个自定义 DependencyProperty
的 UserControl
(ColumnsCount
、RowsCount
):
public partial class CabinetGrid : UserControl
{
public static readonly DependencyProperty ColumnsCountProperty =
DependencyProperty.Register("ColumnsCount", typeof (int), typeof (CabinetGrid));
public static readonly DependencyProperty RowsCountProperty =
DependencyProperty.Register("RowsCount", typeof (int), typeof (CabinetGrid));
public int ColumnsCount
{
get { return (int) GetValue(ColumnsCountProperty); }
set { SetValue(ColumnsCountProperty, value); }
}
public int RowsCount
{
get { return (int) GetValue(RowsCountProperty); }
set { SetValue(RowsCountProperty, value); }
}
}
这是 DataBinding
:
<view:CabinetGrid Grid.Column="1" Grid.Row="2" x:Name="GridRack" ColumnsCount="{Binding SelectedRoom.ColumnCount}" />
而 window 的 DataContext
有一个 属性 SelectedRoom
调用 PropertyChanged-Event
.
通过调试,我知道 UserControl
的 DataContext
设置正确。
但是,当 SelectedRoom
发生变化时(=> 我在列表中选择了另一个项目),我的 UserControl
的 DependencyProperty
ColumnsCount
不会更新。
我很沮丧,因为我已经花了一整天的时间调试这个意想不到的狗屎,使用像 XAMLSpy
和 WpfSpoon
.
这样的工具
请帮忙。
编辑:
Clemens 已经指出,CLR-Property
中包含 DependencyProperty
(ColumnsCount
) 的断点 未触发 。这是一个主要问题,因为我必须在更改时调用一些方法。我正在尝试使用 PropertyChangedCallback
,但目前遇到了一些错误。
为了获得有关依赖项 属性 的值更改的通知,您应该在注册 属性.
时在 PropertyMetadata 中指定 PropertyChangedCallbackpublic static readonly DependencyProperty ColumnsCountProperty =
DependencyProperty.Register(
"ColumnsCount", typeof(int), typeof(CabinetGrid),
new PropertyMetadata(OnColumnsCountPropertyChanged));
private static void OnColumnsCountPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var cabinetGrid = (CabinetGrid)obj;
// do something with the CabinetGrid instance
}