我构建了一个UserControl
,其依赖属性如下:
public MyUserContol()
{
InitializeComponent();
SelectedString = "Defalut";
}
public string SelectedString
{
get { return (string)GetValue(SelectedStringProperty); }
set { SetValue(SelectedStringProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedString. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedStringProperty =
DependencyProperty.Register("SelectedString", typeof(string), typeof(MyUserContol),
new FrameworkPropertyMetadata(OnSelectedStringPropertyChanged));
private static void OnSelectedStringPropertyChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
(source as MyUserContol).SelectedSatringChanged();
}
当我使用它时,它工作正常
<UserContol:MyUserControl SelectedClient="blabla" />
但在DataTemplate
它不起作用!
<DataGrid1:DataGrid x:Name="dg" ItemsSource="{Binding MyDataTable}">
<DataGrid1:DataGrid.Columns>
<DataGrid1:DataGridTemplateColumn SortMemberPath="[Client]" Header="Date" >
<DataGrid1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<UserContol:MyUserControl SelectedClient="blabla" >
</DataTemplate>
</DataGrid1:DataGridTemplateColumn.CellTemplate>
</DataGrid1:DataGrid.Columns>
</DataGrid1:DataGrid>
它没有将值改为“blabla”。我知道它不起作用,因为OnSelectedStringPropertyChanged
没有被调用!该属性保持构造函数给出的默认值。
为什么不起作用?
答案 0 :(得分:0)
我找到了!!!
我需要从Ctor中删除此代码:
SelectedString = "Defalut";