自定义依赖属性 - 绑定不起作用
我正在准备自定义用户控件,它将位于 DataGrid
中。此控件的 DataContext
将是行 ID(来自数据库)和位于 DataGrid
下的 ComboBox
的值。
这就是我将控件嵌入到 DataGrid 中的方式:
<datagrid:DataGridTemplateColumn>
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<nmspc:MyControl IdT="{Binding id}" BValue="{Binding SelectedValue, ElementName=MyComboBox}" />
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
</datagrid:DataGridTemplateColumn>
我想要绑定的值是 MyComboBox 的 id 和选择。 这就是 MyControl 代码背后的样子:
public static readonly DependencyProperty IdTProperty = DependencyProperty.Register("IdT", typeof(int), typeof(MyControl), new PropertyMetadata(IdTChanged));
private static void IdTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//BoxResult obj = d as MyControl;
MessageBox.Show(e.NewValue.ToString());
//obj.IdT = (int)e.NewValue;
}
public int IdT
{
set {SetValue(IdTProperty, value); }
get {return (int)GetValue(TEIdProperty); }
}
public static readonly DependencyProperty BValueProperty = DependencyProperty.Register("BValue", typeof(string), typeof(MyControl), new PropertyMetadata(IdTChanged));
private static void BValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//BoxResult obj = d as MyControl;
MessageBox.Show(e.NewValue.ToString());
//obj.IdT = (string)e.NewValue;
}
public int BValue
{
set {SetValue(BValueProperty, value); }
get {return (int)GetValue(BValueProperty); }
}
绑定机制不适用于我的代码。我期望回调 IdTChanged 和 BValueChanged 将被调用,但它们不会。 我根据 this 编写此代码。预先感谢所有建议。
问候, Pawel
编辑:
这就是调试输出的样子:
System.Windows.Data Error: BindingExpression path error: 'id' property not found on 'My.MyControls.MyView.DataParams' 'My.MyControls.MyView.DataParams' (HashCode=42491497). BindingExpression: Path='id' DataItem='My.MyControls.MyView.DataParams' (HashCode=42491497); target element is 'My.MyControls.MyView.MyControl' (Name=''); target property is 'IdT' (type 'System.Int32')..
System.Windows.Data Error: BindingExpression path error: 'id' property not found on '852' 'System.Int32' (HashCode=852). BindingExpression: Path='id' DataItem='My.MyControls.MyView.DataParams' (HashCode=42491497); target element is 'My.MyControls.MyView.MyControl' (Name=''); target property is 'IdT' (type 'System.Int32')..
我发现这是RelativeSource 的问题。因此在绑定中我设置了这个值:
RelativeSource={RelativeSource TemplatedParent}
BindingExpression 的问题消失了,但仍然不起作用(IdTChanged 中的 MessageBox 未显示)。
一些建议?
I am preparing Custom UserControl, which will be located in a DataGrid
. DataContext
for this control will be row id (from DB) and Value of ComboBox
located under the DataGrid
.
This is how I embed my control in DataGrid
:
<datagrid:DataGridTemplateColumn>
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<nmspc:MyControl IdT="{Binding id}" BValue="{Binding SelectedValue, ElementName=MyComboBox}" />
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
</datagrid:DataGridTemplateColumn>
Values which I want to bind are id and selection of MyComboBox.
This is how MyControl Code behind looks like:
public static readonly DependencyProperty IdTProperty = DependencyProperty.Register("IdT", typeof(int), typeof(MyControl), new PropertyMetadata(IdTChanged));
private static void IdTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//BoxResult obj = d as MyControl;
MessageBox.Show(e.NewValue.ToString());
//obj.IdT = (int)e.NewValue;
}
public int IdT
{
set {SetValue(IdTProperty, value); }
get {return (int)GetValue(TEIdProperty); }
}
public static readonly DependencyProperty BValueProperty = DependencyProperty.Register("BValue", typeof(string), typeof(MyControl), new PropertyMetadata(IdTChanged));
private static void BValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//BoxResult obj = d as MyControl;
MessageBox.Show(e.NewValue.ToString());
//obj.IdT = (string)e.NewValue;
}
public int BValue
{
set {SetValue(BValueProperty, value); }
get {return (int)GetValue(BValueProperty); }
}
Binding mechanism isn't working with my code. I am expecting that callbacks IdTChanged and BValueChanged will be called, but they won't.
Writing this code I based on this. Thanks in advance for all sugestions.
Regards,
Pawel
Edit:
This is how looks debug output:
System.Windows.Data Error: BindingExpression path error: 'id' property not found on 'My.MyControls.MyView.DataParams' 'My.MyControls.MyView.DataParams' (HashCode=42491497). BindingExpression: Path='id' DataItem='My.MyControls.MyView.DataParams' (HashCode=42491497); target element is 'My.MyControls.MyView.MyControl' (Name=''); target property is 'IdT' (type 'System.Int32')..
System.Windows.Data Error: BindingExpression path error: 'id' property not found on '852' 'System.Int32' (HashCode=852). BindingExpression: Path='id' DataItem='My.MyControls.MyView.DataParams' (HashCode=42491497); target element is 'My.MyControls.MyView.MyControl' (Name=''); target property is 'IdT' (type 'System.Int32')..
I found that is problem with RelativeSource. So in binding I set this value:
RelativeSource={RelativeSource TemplatedParent}
Problem with BindingExpression disappeard, but still it doesn't work (MessageBox in IdTChanged is not shown).
Some suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论