我想我的列表綁定到一個ComboBox:綁定列表到一個ComboBox
private BindingList<Tool> toolList = new BindingList<Tool>();
XAML的綁定:
<ComboBox ItemsSource="{Binding toolList}" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedValue= "{Binding toolList}" Height="22"
Name="comboBoxTools" Width="185" SelectionChanged="comboBoxTools_SelectionChanged" />
列表的對象有成員名稱和路徑,我想該名稱出現在組合框中。 當我添加一個新的對象添加到列表不顯示在組合框:
public ObservableCollection<Tool> toolList = new ObservableCollection<Tool>();
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
InputDialog input = new InputDialog();
input.ShowDialog();
inputNewTool = input.enteredTxt;
if (inputNewTool != null)
{
System.Windows.Forms.MessageBox.Show("Chose the Tool's directory");
dlg.DefaultExt = ".exe";
dlg.Filter = "Application (.exe)|*.exe";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Tool tool = new Tool();
tool.Name = inputNewTool;
tool.Path = dlg.FileName;
toolList.Add(tool);
//comboBoxTools.Items.Add(tool);
}
}
}
XAML的綁定:
<ComboBox ItemsSource="{Binding Path=toolList}" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedValue= "{Binding Path=toolList}" Height="22"
Name="comboBoxTools" Width="185" SelectionChanged="comboBoxTools_SelectionChanged" />
現在而不是BindingList嘗試和ObservableCollection – Paparazzi
我將List替換爲ObservableCollection並且沒有任何更改: ( – LittleProgrammer
你確實知道你需要一個公共屬性來進行綁定嗎? – Paparazzi