如何获取列表的名称< string> from List< List< string>>绑定它动态? [英] How to get name of a List<string> from List<List<string>> to bind it dynamically?

查看:156
本文介绍了如何获取列表的名称< string> from List< List< string>>绑定它动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态DataGrid其中1列包含ComboBox模板。现在我得到'N'个组合框。每个ComboBox应该有不同的ItemsSource。怎么可以实现呢?
我的动态datagrid有属性ItemsSourceBinding。现在我需要在运行时为此属性提供一个DataContext.BindingName。如何实现?

I have a dynamic DataGrid in Which 1 column contains ComboBox template. Now I'll get 'N' number of comboboxes. Each ComboBox should have different ItemsSource. how can it be achieved? My dynamic datagrid has the property ItemsSourceBinding. Now I need to provide a DataContext.BindingName to this property at runtime. How can it be achieved?

column.ItemsSourceBinding = new Binding() 
{ 
    Path = new System.Windows.PropertyPath("DataContext." + bindStreamList1),
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGrid), 1) 
}; 

代替 bindStreamList1 列表< string> 。它可以来自 List< List< string>> 或者来自 Dictionary< string,List< string>>

In place of bindStreamList1 I need a name of List<string>. it may be from List<List<string>> or from Dictionary<string,List<string>>

推荐答案

我建议您熟悉MVVM模式。 web上有很多教程。如果你想有双向绑定,你应该实现 INotifyPropertyChanged 接口。你可以发现自己也很好的教程。我也建议在XAML中做绑定,而不是在代码后面。

I would recommend you to get familiar with MVVM pattern. There are tons of tutorials on the web.If you want to have two way binding you should implement the INotifyPropertyChanged interface. You can find yourself also very good tutorials on that. I also would recommend to do your bindings in XAML and not in code behind when ever possible.

这是一个我想要的例子:

Here is an example of what I guess you want:

XAML:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid>
        <DataGrid ItemsSource="{Binding }"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=AvailableNames}" 
                                      SelectedItem="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Path=Name, Mode=OneWay}"
                                    IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

代码背后:

public MainWindow()
{
    InitializeComponent();


    List<MyViewModel1> Items = new List<MyViewModel1>();
    Items.Add(new MyViewModel1() { Name = "Leonardo" , AvailableNames = new List<string>() { "Leonardo", "Michael Angelo" } });
    Items.Add(new MyViewModel1() { Name = "Michael Angelo", AvailableNames = new List<string>() {  "Michael Angelo"} }); // can't make a leonardo out of this item
    Items.Add(new MyViewModel1() { Name = "Master Splinter", AvailableNames = new List<string>() { "Master Splinter", "Jon Skeet" } }); // master stays master PERIOD ..... or become Jon Skeet
    DataContext = Items;

}

而MyViewModel1

And the MyViewModel1

public class MyViewModel1 : INotifyPropertyChanged
{
    private List<string> _availableNames;
    private string _name;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public List<string> AvailableNames
    {
        get
        {
            return _availableNames;
        }
        set
        {
            _availableNames = value;
            OnPropertyChanged();
        }
    }
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这篇关于如何获取列表的名称&lt; string&gt; from List&lt; List&lt; string&gt;&gt;绑定它动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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