以ComboBox为项目模板的ListView,当ComboBox中的选择发生变化时,不要更新源

本文关键字:ComboBox 变化 更新 项目 ListView 选择 | 更新日期: 2023-09-27 18:14:46

我将ListView绑定到ObservableCollection<string>,每个项目应该显示为ComboBox。问题是,当我改变ComboBox中的选择时,它不会更新ObservableCollection<string>。下面是xaml:

<ListView ItemsSource="{Binding CellValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.Column.CellValueChoices, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" SelectedIndex="0" IsEditable="True">
                            <i:Interaction.Behaviors>
                                <behaviors:CellFocusBehavior/>
                                <behaviors:FocusOnLoadBehavior/>
                            </i:Interaction.Behaviors>
                            <ComboBox.InputBindings>
                                <KeyBinding Command="{Binding Path=DataContext.ValidateAndInsertNewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Key="Tab"/>
                            </ComboBox.InputBindings>
                        </ComboBox>
                    </DataTemplate>
                </ListView.ItemTemplate>
</ListView>

以ComboBox为项目模板的ListView,当ComboBox中的选择发生变化时,不要更新源

对于ListView, DataContext是包含ObservableCollection<string> CellValueCellViewModel对象。

看着你的xaml,在我看来,你认为"ItemsSource="{Binding CellValue, Mode=TwoWay"是将wpf控件绑定到它相应的VM属性的方式,这是不完全正确的。Itemsource绑定应该是单向的,因为ViewModel以单向方式将实现INotifyCollectionChanged的列表加载到GUI中。将ItemsSource绑定到TwoWay并不会使GUI知道如何将DataTemplate中的所有控件的值更新回ViewModel。

相反,你应该将每个控件绑定到你的ViewModel属性,以便做到这一点。我给你做了一个样品。

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class OrderViewModel : ViewModelBase
{
    public ObservableCollection<string> ComboBoxOptions { get; set; }
    private string orderId;
    private string instrumentId;
    private string selectedComboOption;
    public OrderViewModel()
    {
        ComboBoxOptions = new ObservableCollection<string>
        {
            "Option1",
            "Option2",
            "Option3",
        };            
    }
    public string OrderId
    {
        get { return orderId; }
        set
        {
            orderId = value;
            OnPropertyChanged();
        }
    }
    public string InstrumentId
    {
        get { return instrumentId; }
        set
        {
            instrumentId = value;
            OnPropertyChanged();
        }
    }
    public string SelectedComboOption
    {
        get { return selectedComboOption; }
        set
        {
            selectedComboOption = value;
            OnPropertyChanged();
        }
    }
}
[Export]
public class MainViewModel : ViewModelBase
{
    private OrderViewModel selectedOrder;
    public ObservableCollection<OrderViewModel> Orders { get; set; }        
    public MainViewModel()
    {
        Orders = new ObservableCollection<OrderViewModel>
        {
            new OrderViewModel {OrderId = "Order1", InstrumentId = "Instrument1"},
            new OrderViewModel {OrderId = "Order2", InstrumentId = "Instrument2"},
            new OrderViewModel {OrderId = "Order2", InstrumentId = "Instrument3"}
        };
    }
    public OrderViewModel SelectedOrder
    {
        get { return selectedOrder; }
        set
        {
            selectedOrder = value;
            OnPropertyChanged();
        }
    }
}
<Window x:Class="WpfTestProj.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfTestProj"
    mc:Ignorable="d" 
    d:DataContext="{d:DesignInstance Type=local:MainViewModel, IsDesignTimeCreatable=False}"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView ItemsSource="{Binding Orders}" SelectedItem="{Binding SelectedOrder}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBox Text="{Binding OrderId}" />
                    <TextBox Text="{Binding InstrumentId}" />
                    <ComboBox ItemsSource="{Binding ComboBoxOptions}"                                  
                              SelectedItem="{Binding SelectedComboOption}"/>
                </StackPanel>                    
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>