簡體   English   中英

綁定到 ObservableCollection (WPF) 時無法更改 ComboBox 選擇

[英]Can't change ComboBox selection when bound to ObservableCollection (WPF)

我正在嘗試創建一個編輯表單來編輯一組自定義電視劇對象的屬性。 其中一個屬性包含將在ComboBox顯示的特定系列的所有擁有的媒體格式(DVD、藍光等)的集合。 項目被添加到ComboBox通過一個單獨的彈出窗口和物品都予以除名ComboBox通過選擇該項目,並單擊刪除Button

我可以向 MediaOwned ComboBox添加新條目就好了,但是當我嘗試選擇特定的ComboBox項目來測試刪除Button我發現我只能選擇第一個條目。 有人可以告訴我我是否遺漏了一些令人尷尬的明顯內容,謝謝。

這是有問題的財產:

    private ObservableCollection<string> _mediaOwned = new ObservableCollection<string>();
    public ObservableCollection<string> MediaOwned
    {
        get { return _mediaOwned; }
        set
        {
            _mediaOwned = value;
            OnPropertyChanged(new PropertyChangedEventArgs("MediaOwned"));
        }
    }

以下是其他相關代碼:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Create binding for the ListBox.
        Binding listBinding = new Binding();
        listBinding.Source = show.Series;
        listBinding.Mode = BindingMode.OneWay;
        listBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        lbSeries.SetBinding(ListBox.ItemsSourceProperty, listBinding);

        // Create binding for the ComboBox.
        Binding myBinding = new Binding();
        myBinding.Path = new PropertyPath("MediaOwned");
        myBinding.Mode = BindingMode.TwoWay;
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        cbMediaOwned.SetBinding(ComboBox.ItemsSourceProperty, myBinding);
    }

    private void btnRemoveMedia_Click(object sender, RoutedEventArgs e)
    {
        Series series = (Series)lbSeries.SelectedItem;
        series.MediaOwned.Remove(cbMediaOwned.Text);
    }

這是 XAML 代碼:

        <Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Left" Margin="0,8,8,0" 
                DataContext="{Binding ElementName=lbLists, Path=SelectedItem}">
            <DockPanel VerticalAlignment="Top">
                <StackPanel>
                    <ListBox x:Name="lbSeries" Style="{StaticResource BasicListStyle}" Width="180" Height="300" 
                             DisplayMemberPath="Title" SelectionMode="Single" LayoutUpdated="lbSeries_LayoutUpdated">
                    </ListBox>
                </StackPanel>
                
                <StackPanel x:Name="editPanel" DataContext="{Binding ElementName=lbSeries, Path=SelectedItem}">
                    <StackPanel  Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 4, 0, 0">
                        <TextBlock Style="{StaticResource SmallFont}" Width="100">Title</TextBlock>
                        <TextBox x:Name="txtTitle" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="8, 8, 16, 8"></TextBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <TextBlock Style="{StaticResource SmallFont}" Width="100">Media owned</TextBlock>

                        <ComboBox x:Name="cbMediaOwned" Style="{StaticResource ComboBoxStyle}"  Width="150" Margin="8,8,6,8" 
                                ></ComboBox>

                        <Button x:Name="btnAddMedia" Style="{StaticResource ToolbarButtonStyle}" Click="btnAddMedia_Click" Margin="0">
                            <StackPanel ToolTip="Add media">
                                <Image Source="Images/add.png" />
                            </StackPanel>
                        </Button>
                        <Button x:Name="btnRemoveMedia" Style="{StaticResource ToolbarButtonStyle}" Click="btnRemoveMedia_Click" Margin="4">
                            <StackPanel ToolTip="Remove media">
                                <Image Source="Images/remove.png" />
                            </StackPanel>
                        </Button>
                    </StackPanel>
                </StackPanel>
            </DockPanel>
        </Border>

或者,我也可以刪除后面代碼中的綁定代碼,並用下面的代碼替換ComboBox (但我仍然遇到同樣的問題 - 我無法在ComboBox選擇任何內容):

                    <ComboBox x:Name="cbMediaOwned" Style="{StaticResource ComboBoxStyle}"  Width="150" Margin="8,8,6,8" ItemsSource="{Binding ElementName=lbSeries, Path=SelectedItem.MediaOwned, UpdateSourceTrigger=PropertyChanged}" 
                              SelectedItem="{Binding SelectedMedia, UpdateSourceTrigger=PropertyChanged}"></ComboBox>

SelectedMedia 屬性:

    private string _selectedMedia = "";
    public string SelectedMedia
    {
        get { return _selectedMedia; }
        set
        {
            _selectedMedia = value;
            OnPropertyChanged(new PropertyChangedEventArgs("SelectedMedia"));
        }
    }

這是我的xaml:

<ComboBox x:Name="Models_ComboBox"
      Width="110"
      Text="Model"
      ItemsSource="{Binding Models}"
      SelectedItem="{Binding SelectedModel}"
      DisplayMemberPath="Model"
      MouseDoubleClick="Models_ComboBox_MouseDoubleClick"
      SelectionChanged="Models_ComboBox_SelectionChanged"/>

這是我的 VM 屬性:

private DataTable models;
public DataTable Models
{
    get { return models; }
    set
    {
        if (models != value)
        {
            models = value;
            OnPropertyChanged(nameof(Models));
        }
    }
}

private DataRowView selectedModel;
public DataRowView SelectedModel
{
    get { return selectedModel; }
    set
    {
        if (selectedModel != value)
        {
            selectedModel = value;
            if (value != null)
            {
                InitializeOptions(value["Model"].ToString());
            }
            OnPropertyChanged(nameof(SelectedModel));
        }
    }
}

如您所見,ComboBox 的 ItemsSource 和 SelectedItem 綁定到 ViewModel 中的兩個不同屬性。 ItemsSource 綁定到從數據庫填充的 DataTable。 一旦用戶選擇了一個模型,就會有其他選項組合框根據該選擇進行填充。

自己解決了這個問題。 我有一行代碼在我沒有意識到的情況下自動設置了ComboBoxSelectedIndex

暫無
暫無

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

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