簡體   English   中英

當ComboBox中的選擇更改時,以ComboBox作為項目模板的ListView不會更新源

[英]ListView with ComboBox as item template don't update source when selection in ComboBox changes

我將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>

ListView DataContextCellViewModel對象,包含ObservableCollection<string> CellValue

看着您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM