我有一个 JobItem 对象,在它里面,我有:
public ObservableCollection<string> BusinessUnit
{
get
{
return businessUnit;
}
set { businessUnit = value; }
}
现在用户需要填写一个表格并添加多个业务单位。我创建了一个列表框,旁边有添加和删除按钮。添加按钮打开一个对话框,提示用户添加业务单位的名称,然后将其添加到列表框中。这是我为此编写的代码:
<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" />
在后面的代码中,我将 JobItem 绑定到表单,因为我有其他文本框绑定到其他 JobItems(绑定适用于此)。
public NewJobDialog(int workOrderCounter)
{
InitializeComponent();
item = new JobItem();
base.DataContext = item();
businessUnitBox.DataContext = item.BusinessUnit;
}
然后当我点击添加按钮添加一个业务部门时,我有这个代码:
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);
}
}
现在当我运行程序时,每次我添加一个业务单位时,它都不会出现在列表框中。请给我一些示例代码,以了解如何使其工作。谢谢。