简体   繁体   English

将Listbox绑定到ObservableCollection <T> 不工作的WPF

[英]Binding Listbox to ObservableCollection<T> not working WPF

I have a JobItem object, and inside it, I have: 我有一个JobItem对象,在其中,我有:

public ObservableCollection<string> BusinessUnit
    {
        get 
        { 
            return businessUnit; 
        }
        set { businessUnit = value; } 

    }

Now the user needs to fill in a form and add in multiple Business Units. 现在,用户需要填写表单并添加多个业务单位。 I have created a listbox that has add and delete buttons beside it. 我创建了一个列表框,旁边有添加和删除按钮。 The add button opens a dialog that prompts the user to add the name of the business unit, then adds it to the list box. 添加按钮打开一个对话框,提示用户添加业务单位的名称,然后将其添加到列表框中。 This is the code I have for this: 这是我的代码:

<my:ValidatingListBox Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="5" Grid.RowSpan="1" x:Name="businessUnitBox" SelectionMode="Multiple" SelectionChanged="ValidatingListBox_SelectionChanged" IsSynchronizedWithCurrentItem="True">
            <my:ValidatingListBox.ItemsSource>
                <Binding Source="{StaticResource jobItemDataSource}" Path="BusinessUnit" Mode="TwoWay"/>
            </my:ValidatingListBox.ItemsSource>
            </my:ValidatingListBox>

    <Button Style="{StaticResource addBtnStyle}" Grid.Column="2" Grid.Row="5" Name="addBusinessUnitBtn" Click="addBusinessUnitBtn_Click" />
    <Button Style="{StaticResource removeBtnStyle}" Grid.Column="2" Grid.Row="5" Name="delBusinessUnitBtn" Click="delBusinessUnitBtn_Click" />

In the code behind, I'm binding the JobItem to the form because I have other textboxes that are bound to other JobItems (the binding works for this). 在后面的代码中,我将JobItem绑定到表单,因为我有其他文本框绑定到其他JobItems(绑定适用于此)。

public NewJobDialog(int workOrderCounter)
    {
        InitializeComponent();
        item = new JobItem();
        base.DataContext = item();
        businessUnitBox.DataContext = item.BusinessUnit;
     }

Then when I click on the add button to add a business unit, I have this code: 然后,当我点击添加按钮添加业务单位时,我有以下代码:

private void addBusinessUnitBtn_Click(object sender, RoutedEventArgs e)
    {
        AddBusinessUnitDialog addBusinessUnit = new AddBusinessUnitDialog();
        addBusinessUnit.ShowDialog();

        if (addBusinessUnit.DialogResult == true)
        {
            item.BusinessUnit.Add(addBusinessUnit.BusinessUnit());
        }

    }

    private void delBusinessUnitBtn_Click(object sender, RoutedEventArgs e)
    {
        if (businessUnitBox.Items.Count > 0)
        {
            item.BusinessUnit.Remove((string)businessUnitBox.SelectedItem);
        }

    }

Now when I run the program, every time I add a business unit, it does not show up in the listbox. 现在,当我运行程序时,每次添加业务单位时,它都不会显示在列表框中。 Please give me some type of sample code as to how I would be able to get this to work. 请给我一些类型的示例代码,以便我能够如何使其工作。 Thanks. 谢谢。

I believe it is because you need to set the DataContext to the parent class. 我相信这是因为您需要将DataContext设置为父类。

businessUnitBox.DataContext = item;

This is because you have the following binding 这是因为您有以下绑定

<Binding Source="{StaticResource jobItemDataSource}" Path="BusinessUnit" Mode="TwoWay"/>

Additionally you can simplify the binding by having.. 此外,您可以通过...来简化绑定。

<my:ValidatingListBox ItemsSource="{Binding BusinessUnit}" ... />

EDIT: 编辑:

If you really want 如果你真的想要

businessUnitBox.DataContext = item.BusinessUnit;

Then your binding needs to be 然后你的绑定需要

<my:ValidatingListBox ItemsSource="{Binding Path=." ... />

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

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