如何获取列表的名称从List< List< string>比;来动态绑定它

本文关键字:List string lt 何获取 动态绑定 列表 获取 | 更新日期: 2023-09-27 18:13:08

我有一个动态DataGrid,其中1列包含ComboBox模板。现在我将得到N个组合框。每个组合框应该有不同的ItemsSource。怎样才能实现呢?我的动态数据网格具有ItemsSourceBinding属性。现在我需要提供一个DataContext。在运行时绑定到此属性。怎样才能实现呢?

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

我需要一个名称List<string>来代替bindStreamList1。可能来自List<List<string>>Dictionary<string,List<string>>

如何获取列表的名称<string>从List< List< string>比;来动态绑定它

我建议您熟悉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));
        }
    }
}