[英]changing the bound object in a datatemplate
Data templates are great, but I'm having a problem with binding in a particular situation. 数据模板很棒,但是在特定情况下绑定存在问题。 I have a class, Value, that has various descendants like
StringValue
, DateValue
, etc. These Values show up in a Listbox
. 我有一个Value类,它具有
StringValue
, DateValue
等各种后代。这些值显示在Listbox
。 This template works fine, binding to a specific property of StringValue: 此模板可以正常工作,并绑定到StringValue的特定属性:
<DataTemplate DataType="{x:Type values:StringValue}">
<TextBox Margin="0.5"
Text="{Binding Path=Native}" />
</DataTemplate>
However, when I bind to an object itself, instead of a specific property, the changes don't update the object, as in this template: 但是,当我绑定到对象本身而不是特定属性时,更改不会更新该对象,如以下模板所示:
<DataTemplate DataType="{x:Type values:LookupValue}">
<qp:IncrementalLookupBox SelectedValue="{Binding Path=., Mode=TwoWay}"
LookupProvider="{Binding ElementName=EditWindow, Path=ViewModel.LookupProvider}">
</qp:IncrementalLookupBox>
</DataTemplate>
IncrementalLookupBox
is a UserControl
that ultimately allows a user to select a LookupValue
, which should replace the item bound in the template. IncrementalLookupBox
是一个UserControl
,它最终允许用户选择LookupValue
,它应该替换模板中绑定的项目。 If this was bound to a simple type like an int or string, the binding would replace the object, so I'm not sure what the difference is with a more complex object. 如果将其绑定到一个简单的类型(例如int或字符串),则该绑定将替换该对象,因此我不确定更复杂的对象有什么区别。 I know that the
IncrementalLookBox
is working, because binding some textboxes to the properties of SelectedValue
(which is a dependency property) shows the correctly selected LookupValue
. 我知道
IncrementalLookBox
可以正常工作,因为将某些文本框绑定到SelectedValue
的属性(这是一个依赖属性)会显示正确选择的LookupValue
。
In case it makes the situation more clear, here is the implementation of SelectedValue: 如果情况变得更加清楚,这里是SelectedValue的实现:
public LookupValue SelectedValue
{
get { return (LookupValue)GetValue(SelectedValueProperty); }
set { SetValue(SelectedValueProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register("SelectedValue", typeof(LookupValue), typeof(IncrementalLookupBox), new PropertyMetadata(OnSelectedValuePropertyChanged));
private static void OnSelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = d as IncrementalLookupBox;
obj.OnSelectedValuePropertyChanged(e);
}
private void OnSelectedValuePropertyChanged(DependencyPropertyChangedEventArgs e)
{
CheckForSelectedValueInLookups();
}
如果所有其他方法均失败,请考虑使用ValueConverter来获取所需的值。
Edit: this does not work. 编辑:这不起作用。 See link in comments below.
请参阅下面评论中的链接。
Make sure your class implements INotifyPropertyChanged and raise PropertyChanaged here: 确保您的类实现了INotifyPropertyChanged并在此处引发PropertyChanaged:
private void OnSelectedValuePropertyChanged(DependencyPropertyChangedEventArgs e)
{
CheckForSelectedValueInLookups();
// RaisePropertyChanged();
}
My issue is the same as described here: 我的问题与此处描述的相同:
WPF TwoWay Binding of ListBox using DataTemplate WPF使用DataTemplate的ListBox双向绑定
Apparently if I don't write enough text here, my answer will be converted to a comment and not close out the question. 显然,如果我在此处输入的文字不够多,我的答案将被转换为评论,而无法解决问题。 So, to summarize the issue, a two-way Binding=.
因此,总而言之,使用双向Binding =。 in a datatemplate used in a ListBox (or any ItemsControl I image) won't work, because it is not the object itself being bound, but the ListBoxItem that contains it.
在ListBox(或任何ItemsControl I图像)中使用的数据模板中,它将不起作用,因为它不是绑定的对象本身,而是包含它的ListBoxItem。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.