简体   繁体   中英

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 . This template works fine, binding to a specific property of 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. 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. 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 .

In case it makes the situation more clear, here is the implementation of 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:

 private void OnSelectedValuePropertyChanged(DependencyPropertyChangedEventArgs e)
 {
     CheckForSelectedValueInLookups();
     // RaisePropertyChanged();
 }

My issue is the same as described here:

WPF TwoWay Binding of ListBox using DataTemplate

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=. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM