将ObservableCollection绑定到DataGrid ComboBox? [英] Binding ObservableCollection to DataGrid ComboBox?

查看:142
本文介绍了将ObservableCollection绑定到DataGrid ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ComboBox的项目与ObservableCollection绑定,我已成功将其绑定到 StaticResource

I am trying to bind the items of the ComboBox with an ObservableCollection, I have been successfully binding them to a StaticResource,

XAML, Nursing_Home_Name 是SQL Server表,此代码对我来说很好用。

The XAML, Nursing_Home_Name is the SQL Server Table and this code works fine for me.

<Window.Resources>
    <local:CollectionsLists x:Key="StaticNursingHomeList" />
</Window.Resources>


<DataGridTemplateColumn Width="120" Header="施設名称">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
                           Path=Nursing_Home_Name}" />
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
          <ComboBox Text="{Binding Path=Nursing_Home_Name,
                           UpdateSourceTrigger=PropertyChanged}"
                           ItemsSource="{Binding NursingHome, 
                           Source={StaticResource StaticNursingHomeList}}" />
       </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

和后面的代码,

class CollectionsLists
    {     
        public static List<string> NursingHome { get; set; } 
        var homeNames = Database.GetNursingHomeNames(_nursingHomeSection);
        if (homeNames != null)
        {
           NursingHome = new List<string>();
           foreach (var item in homeNames)
           {
                NursingHome.Add(item);
           }
        }
    }






现在输入问题代码

我发现很难调试XAML以解决我做错的事情。

I am finding it very hard to debug the XAML on what I am doing wrong.

这是后面的代码,

public partial class MainWindow : INotifyPropertyChanged
    {        
        public MainWindow()
        { 
            DataContext = this; 
            InitializeComponent();
        }

        private ObservableCollection<string> _nursingHomeNames = new ObservableCollection<string>();

        public ObservableCollection<string> NursingHomeNames
           {
               get { return _nursingHomeNames; }
               set
               {
                   _nursingHomeNames = value;
                   NotifyPropertyChanged();
               }
           }
}

我无法显示众多尝试使XAML正常工作的几种方法,但是我将发布我认为最接近使其工作的方法,

And I cannot show the multitude of ways that I have tried to get the XAML to work, but instead I will post what I thought was the closest to getting it to work,

XAML, Nursing_Home_Name 是SQL Server表BTW

The XAML, Nursing_Home_Name is the SQL Server Table BTW

<DataGridTemplateColumn Width="120" Header="施設名称">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
                           Path=Nursing_Home_Name}" />
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
          <ComboBox Text="{Binding Path=Nursing_Home_Name,
                           UpdateSourceTrigger=PropertyChanged}"
                           ItemsSource="{Binding NursingHomeNames}" />
       </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

我需要添加什么才能使其正确绑定?

推荐答案


我需要添加些什么才能使其正确绑定?

What do I need to add to get this to bind correctly?

由于NursingHomeNames是父窗口的属性,而ComboBox的DataContext是DataGrid的ItemsSource集合中的一项,因此需要将窗口指定为绑定的来源。试试这个:

Since NursingHomeNames is a property of the parent window and the DataContext of the ComboBox is an item in the ItemsSource collection of the DataGrid, you need to specify the window as the source of the binding. Try this:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox ItemsSource="{Binding Path=NursingHomeNames, RelativeSource={RelativeSource AncestorType=Window}}"
                          SelectedItem="{Binding Path=Nursing_Home_Name}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

这篇关于将ObservableCollection绑定到DataGrid ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆