绑定到 ObservableCollection (WPF)

时间:2021-07-12 15:24:41

标签: c# wpf

我正在尝试创建一个编辑表单,用于编辑一组自定义电视剧对象的属性。其中一个属性包含将在 ComboBox 中显示的特定系列的所有拥有的媒体格式(DVD、Blu-ray 等)的集合。项目通过单独的弹出窗口添加到 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"));
        }
    }

2 个答案:

答案 0 :(得分:0)

这是我的 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。一旦用户选择了一个模型,就会有其他选项组合框根据该选择进行填充。

答案 1 :(得分:0)

自己解决了这个问题。我有一行代码会在我没有意识到的情况下自动设置 SelectedIndexComboBox

相关问题