如何将字符串ObservableCollection绑定到wpf DataGridTemplateColumn中的combobox。 [英] How to bind a string ObservableCollection to combobox in wpf DataGridTemplateColumn.

查看:121
本文介绍了如何将字符串ObservableCollection绑定到wpf DataGridTemplateColumn中的combobox。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我无法弄清楚我做错了什么。我的模型中有以下代码。

  private  ObservableCollection< string> _metadataFieldList =  new  ObservableCollection< string>(); 
public ObservableCollection< string> MetadaFieldList
{
get { return _metadataFieldList; }
set
{
_metadataFieldList = value ;
}
}



视图模型中的以下代码

列表< string> ; lst =  new  DirectoryInfo(fullFolderPath).GetDirectories(  * ,SearchOption.AllDirectories)
.Select(x = > x.FullName)。ToList();

model.MetadaFieldList = new ObservableCollection< string>(lst);





和我的xaml标记中的以下内容。

 <   DataGridTemplateColumn    宽度  = 自动 >  
< DataGridTemplateColumn.CellTemplate >
< DataTemplate >
< ComboBox SelectedValue = {Binding Path = FieldName,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}

< span class =code-attribute>
ItemsSource = {Binding Path = MetadaFieldList} >
< / ComboBox >
< span class =code-keyword>< / DataTemplate >
< / DataGridTemplateColumn.CellTemplate >
< / DataGridTemplateColumn >



组合框始终为空,且ItemsSource为空。



提前感谢您的帮助。



Zafar

解决方案

必须按如下方式更改ItemSource

< pre lang =xml> ItemsSource ={Binding DataContext.TaxonomyList,Mode = TwoWay,
RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type Window}},UpdateSourceTrigger = PropertyChanged}


Hi All,

I am unable to figure out what I am doing wrong. i have the following code in my model.

private ObservableCollection<string> _metadataFieldList = new ObservableCollection<string>();
        public ObservableCollection<string> MetadaFieldList
        {
            get { return _metadataFieldList; }
            set
            {
                _metadataFieldList = value;
            }
        }


The following code in the viewmodel

List<string> lst = new DirectoryInfo(fullFolderPath).GetDirectories("*", SearchOption.AllDirectories)
                .Select(x => x.FullName).ToList();

            model.MetadaFieldList = new ObservableCollection<string>(lst);



and the following in my xaml markup.

<DataGridTemplateColumn Width="Auto">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox SelectedValue="{Binding Path=FieldName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

                                ItemsSource="{Binding Path=MetadaFieldList}">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>


The combobox is always empty and and the ItemsSource is null.

Thanks in advance for any help.

Zafar

解决方案

Had to Change the ItemSource as follows

ItemsSource="{Binding DataContext.TaxonomyList, Mode=TwoWay, 
           RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}"


这篇关于如何将字符串ObservableCollection绑定到wpf DataGridTemplateColumn中的combobox。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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