[英]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}" />
而窗口的DataContext
具有一个属性SelectedRoom
,该属性调用PropertyChanged-Event
。
通过调试,我知道UserControl
的DataContext
设置正确 。
但是,当SelectedRoom
更改时(=>我选择了列表中的另一个项目),我的UserControl
的DependencyProperty
ColumnsCount
不会更新。 我非常沮丧,因为我已经用XAMLSpy
和WpfSpoon
这样的工具整天调试了这个意外的WpfSpoon
。
请帮忙。
编辑:
Clemens已经指出,包装DependencyProperty
( ColumnsCount
)的CLR-Property
中的断点不会触发 。 这是一个主要问题,因为我必须在更改上调用一些方法。 我正在尝试使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.