如何从List <list <string>&gt;获取List <string>的名称动态绑定它?

时间:2016-10-14 11:22:33

标签: c# wpf data-binding combobox datagrid

我有一个动态DataGrid,其中1列包含ComboBox模板。现在我得到N&#39; N&#39;组合框的数量。每个ComboBox应该有不同的ItemsSource。怎么能实现? 我的动态数据网格具有ItemsSourceBinding属性。现在我需要在运行时向此属性提供DataContext.BindingName。如何实现?

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

代替bindStreamList1我需要List<string>的名字。它可能来自List<List<string>>或来自Dictionary<string,List<string>>

1 个答案:

答案 0 :(得分:0)

我建议你熟悉MVVM模式。网上有很多教程。如果你想要双向绑定,你应该实现INotifyPropertyChanged接口。你也可以找到非常好的教程。我还建议你在XAML中进行绑定,而不是在代码背后进行绑定。

以下是我想你想要的一个例子:

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

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