我将我的属性添加到控件中,以便可以通过 xaml 设置它
public class CheckBoxDataGridControl : DisabledRowsDataGrid
{
public string ActionCheckBoxColumnFieldName
{
get
{
return (string)GetValue(ActionCheckBoxColumnFieldNameProperty);
}
set
{
SetValue(ActionCheckBoxColumnFieldNameProperty, value);
}
}
public static readonly DependencyProperty ActionCheckBoxColumnFieldNameProperty;
static CheckBoxDataGridControl()
{
ActionCheckBoxColumnFieldNameProperty = DependencyProperty.Register("ActionCheckBoxColumnFieldName", typeof(string), typeof(CheckBoxDataGridControl));
}
public CheckBoxDataGridControl()
{
...
if (string.IsNullOrWhiteSpace(ActionCheckBoxColumnFieldName))
ActionCheckBoxColumnFieldName = "IsAction";
...
}
在构造函数中,该属性始终为空。如何将 xaml 标记中的内容传递给控件的构造函数?
在构造函数中- 没办法。XAML 只知道如何使用默认构造函数构造对象,并且在构造对象之后设置属性。好吧,别忘了 Dependency Property 的值是直接设置的(绕过通常的属性,这里只是为了在代码中方便使用),所以要对依赖属性的变化做出反应,使用重载
DependencyProperty.RegisterPropertyMetadata接受的宿主和构造函数PropertyMetadataPropertyChangedCallback,在其中已经执行了必要的操作。