【问题标题】:ObservableCollection Binding to a comboBoxObservableCollection 绑定到组合框
【发布时间】:2014-02-18 13:08:59
【问题描述】:

我创建 ObservableCollection:

public ObservableCollection<Tool> toolList = new ObservableCollection<Tool>();

我在构造函数中设置了 DataContext:

public MainWindow()
    {           
       InitializeComponent();
       DataContext = toolList;           
    }

实际:

    public MainWindow()
    {

       InitializeComponent();
       DataContext = this;

    }

公开列表:

    public ObservableCollection<Tool> ToolList
   {
       get { return toolList; }
   }

如何将对象添加到列表中:

    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);
            }                
       }            
     }

我希望只有工具对象的名称显示在组合框中。 XAML:

 <ComboBox ItemsSource="{Binding Path= ToolList}" DisplayMemberPath="Name" 
 SelectedValuePath ="Name" SelectedValue="{Binding Path=ToolList}" Height="22" 
 Name="comboBoxTools" Width="185" IsEditable="False" />

编辑:现在 xaml 显示如下:

   <ComboBox ItemsSource="{Binding Path= ToolList, 
   UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"

工具类:

 public class Tool
{
    public   string Name { get; set; }
    public   string Path { get; set; }

   public Tool() { }
}

我在组合框中看不到任何东西。为什么?这些工具已成功添加到集合中。 我生气了

【问题讨论】:

  • 您的SelectedValue 绑定到与您的ItemsSource 相同的路径。这看起来很可疑。
  • 我删除了选定的值和选定的路径,但这并没有解决问题:/

标签: c# wpf observablecollection


【解决方案1】:

您已将 Window 的 DataContext 设置为 ObservableCollection 本身。

所以 将 ItemsSource 与 DataContext 本身绑定,因为Binding 将自动指向 List 实例。

<ComboBox ItemsSource="{Binding}"/>

理想情况下,您应该将 DataContext 设置为自身:

DataContext = this;

此外,SelectedValue 仅指向列表。这没有任何意义。如果不使用SelectedValuePath,它应该指向类工具的一个实例。如果使用 SelectedValuePath,它应该指向字符串类型的属性。

要么完全删除它,要么将其设置为类中的适当实例。

【讨论】:

  • 第一个提案他不会得到解决方案,因为他对 SelectedValue 的绑定也是错误的。您的第二种方法是正确的,将DataContext 设置为this 并将SelectedValue 更改为另一个属性(我们还不知道)。
  • @Herm - 首先他将在组合框中填充项目。 SelectedValue 部分适用于两种解决方案,而不是特定于第二种解决方案。
  • 是的,只是想提一下,使用第一个绑定(将 DataContext 设置为列表)将 SelectedValue 绑定到 MainWindow 的另一个属性会很麻烦,这在第二个解决方案。当然,第一个会正确填充列表。
  • 好的,我更改了数据上下文并更正了 xaml:
  • @LittleProgrammer - 属性是否仅存在于代码中?
【解决方案2】:

尝试在您的 ItemsSource-Binding 中将 UpdateSourceTrigger 设置为 PropertyChanged

【讨论】:

  • 没有改变:
【解决方案3】:

好的,我在 youtube 上找到了一个简单的解决方案(Data Binding with Windows Presenation Foundation from ht195367)。我没有在 Xaml 中定义 comboBox.ItemsSource,而是在 Window_Loaded 中定义。

comboBoxTools.ItemsSource = toolList;

为了显示我在工具类中实现 ToStringng 的工具名称:

public override string ToString()
{
        return Name;

}

【讨论】:

  • 使用这种方法,您将回到传统的 Winforms 方法,而无需利用 WPF 绑定引擎。休息绝对是你的电话。祝你有美好的一天..!! :)
猜你喜欢
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
相关资源
最近更新 更多