【问题标题】:How to get name of a List<string> from List<List<string>> to bind it dynamically? [closed]如何从 List<List<string>> 获取 List<string> 的名称以动态绑定它? [关闭]
【发布时间】:2017-02-23 19:34:05
【问题描述】:

我有一个动态 DataGrid,其中 1 列包含 ComboBox 模板。现在我将获得“N”个组合框。每个 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&lt;string&gt; 的名称。它可能来自List&lt;List&lt;string&gt;&gt; 或来自Dictionary&lt;string,List&lt;string&gt;&gt;

【问题讨论】:

  • List&lt;string&gt; 没有与之关联的名称,您的意思是要使用字典吗?向我们展示您的代码。
  • column.ItemsSourceBinding = new Binding() { Path = new System.Windows.PropertyPath("DataContext." + bindStreamList1), RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGrid), 1) };代替“bindStreamList1”,我需要一个 List 的名称。它可能来自 List> 或 Dictionary>
  • 欢迎来到 Stack Overflow!请参阅How to Askminimal reproducible example

标签: c# wpf data-binding combobox datagrid


【解决方案1】:

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

【讨论】:

  • 嗯,请再次阅读问题。这显然不是你想要的......更新如下
  • 我完全过度使用了我的答案。现在它应该适合;-)
  • 我根据这个解决方案稍微更改了我的代码并且它起作用了。非常感谢
  • 不客气!无论如何,您可以投票赞成我的回答,而不是说谢谢 ^^
  • 我是 Stackoverflow 的新手,所以我的声望不到 15,因此我无法投票。
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
相关资源
最近更新 更多