简体   繁体   English

WPF C#表示枚举列表为ListBox并将值绑定到组合框不起作用

[英]WPF C# representing List of enums as ListBox and binding the value to combobox doesn't work two way

I have set a ListBox.ItemsSource to an ObservableCollection of custom class and the DataTemplate represents the data as ComboBoxes. 我已将ListBox.ItemsSource设置为自定义类的ObservableCollection,而DataTemplate将数据表示为ComboBoxes。 When the ObservableCollection gets an item added, a new ComboBox gets created inside the ListBox, and if I change a value inside the ObservableCollection the connected ComboBox gets its value updated. 当ObservableCollection获取一个项目时,会在ListBox中创建一个新的ComboBox,如果我更改了ObservableCollection中的值,则连接的ComboBox会更新其值。 However, if I change the ComboBoxes' values nothing happens inside the ObservableCollection. 但是,如果我更改了ComboBox的值,ObservableCollection中没有任何内容。 I have set the value binding of the ComboBox to 我已将ComboBox的值绑定设置为

SelectedItem="{Binding Path=., Mode=TwoWay}"

So in short, I'm trying to connect ComboBoxes to a value inside a list, so that when I change the ComboBox.SelectedItem, the right value is changed inside the list and vice versa. 所以简而言之,我正在尝试将ComboBoxes连接到列表中的值,这样当我更改ComboBox.SelectedItem时,正确的值会在列表中更改,反之亦然。 The problem is that the values inside the list don't get updated when the ComboBox.SelectedItem gets changed. 问题是当ComboBox.SelectedItem发生更改时,列表中的值不会更新。


xaml: XAML:

<ListBox x:Name="ContainerList" Margin="5,0,5,0" Width="140">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ComboBox 
                            ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}},Path=TestEnums}"
                            SelectedItem="{Binding Path=., Mode=TwoWay}"
                            >
                        </ComboBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button x:Name="MoreButton" Content="More" Click="AddContainerClass_Click"/>

Code behind: 代码背后:

 public partial class MainWindow : Window
{
    public enum TestEnum
    {
        value0,
        value1,
        value2,
        value3,
        value4,
    }

    public class ContainerClass
    {
        public ObservableCollection<TestEnum> Enums { get; set; }
        public ContainerClass()
        {
            Enums = new ObservableCollection<TestEnum>();
        }
    }

    public IEnumerable TestEnums { get; set; }
    public ContainerClass Container { get; set; }


    public MainWindow()
    {
        TestEnums = Enum.GetValues(typeof(TestEnum)).Cast<TestEnum>();
        InitializeComponent();
        Container = new ContainerClass();
        ContainerList.ItemsSource = Container.Enums;
        MoreButton.DataContext = Container;
    }

    private void AddContainerClass_Click(object sender, RoutedEventArgs e)
    {
        Button b = sender as Button;
        ContainerClass cc = b.DataContext as ContainerClass;
        cc.Enums.Add(TestEnum.value0);
    }
}

As far as I can tell you are binding to a list of enums (which happens to be in an observable collections), ok. 据我所知,你绑定到一个枚举列表(恰好在一个可观察的集合中),好的。

When an enum is added one sees it on the screen because the binding to the observable collection is smart enough subscribe to the change notification from the observable collection. 当添加枚举时,可以在屏幕上看到它,因为与可观察集合的绑定足够聪明,可以订阅来自可观察集合的更改通知。

But it seems you want the enum in the list to change to a different enum via direct combobox binding; 但似乎您希望列表中的枚举通过直接组合框绑定更改为不同的枚举; that is not possible because the observable collection is not being considered. 这是不可能的,因为没有考虑可观察的集合。

Why? 为什么?

In a sense you are indirectly asking the observable collection to delete the item, then add a new item in its place and then send a messages about each of the changes. 从某种意义上说,您间接要求可观察集合删除该项目,然后在其位置添加新项目,然后发送有关每个更改的消息。 That is not possible with just a binding to the reference of the selected item of the list. 仅仅绑定到列表中所选项的reference是不可能的。

What to do? 该怎么办?

It would be better to simply use the list box, remove that DataTemplate you have and create buttons below it Add , Delete and Replace , possibly with a side drop down combobox as a selection of the enums. 最好只使用列表框,删除你拥有的DataTemplate并在其下面创建按钮AddDeleteReplace ,可能还有一个侧面下拉组合框作为枚举的选择。 then wire up the buttons in the code behind to work with the observable collection directly by removing the selected and replacing it if necessary. 然后通过删除所选的并在必要时替换它来直接连接后面代码中的按钮以直接使用可观察集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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