How to get name of a List<string> from List<List<string>> to bind it dynamically? [closed]

不羁岁月 提交于 2019-12-24 09:14:35

问题


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

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


回答1:


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>

Code behind:

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;

}

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


来源:https://stackoverflow.com/questions/40041968/how-to-get-name-of-a-liststring-from-listliststring-to-bind-it-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!