访问DataGrid中的特定列表列表

时间:2017-06-14 14:28:14

标签: c# wpf xaml datagrid wpfdatagrid

我有一段由List组成的数据,而List又有一个List。如何显示特定的LeafSide?

视图模型:

    private List<Leaf> _Leaves = new List<Leaf>();
    public List<Leaf> Leaves
    {
        get { return _Leaves; }
        set
        {
            _Leaves = value;
            NotifyPropertyChanged();
        }
    }

数据结构:

class Leaf : NotifyBase
{
    private string _LeafNo;
    public string LeafNo
    {
        get { return _LeafNo; }
        set
        {
            _LeafNo = value;
            NotifyPropertyChanged();
        }
    }
    private List<LeafSide> _Side = new List<LeafSide>();
    public List<LeafSide> Side
    {
        get { return _Side; }
        set
        {
            _Side = value;
            NotifyPropertyChanged();
        }
    }
    //More stuff
}
class LeafSide : NotifyBase
{
    private string _Notes;
    public string Notes
    {
        get { return _Notes; }
        set
        {
            _Notes = value;
            NotifyPropertyChanged();
        }
    }
    //More stuff
 }

查看:

<DataGrid ItemsSource="{Binding Leaves}">
<DataGrid.Columns>
    <DataGridTextColumn Width="auto" MinWidth="50" Header="Notes" Binding="{Binding Side/Notes}"/>
    <DataGridTextColumn Width="50" Header="Punch" Binding="{Binding Side/Punch}"/>
    <!--More-->
</DataGrid.Columns>

上面的DataGrid将显示第一个完美的LeafSide,但我不能为我的生活弄清楚如何让它显示第二面。我可以找到DataGrids和嵌套列表的所有示例都是显示列表中的特定值。 我期待像

这样的东西
<DataGridTextColumn Width="auto" MinWidth="50" Header="Notes" Binding="{Binding Side[1]/Notes}"/>

工作,但事实并非如此。

编辑更多信息: 我需要DataGrid来显示: Seperate DataGrids for Leaf and LeafSide[0] displayed 而且对于LeafSide [1]

2 个答案:

答案 0 :(得分:0)

我建议创建一个名为&#39; SelectedSide&#39;在Leaf&#39; Leaf&#39;并使用那个来绑定

  private LeafSide _selectedSide;        
    public LeafSide SelectedSide
    {
        get { return _selectedSide; }
        set { _selectedSide = value; NotifyPropertyChanged("SelectedSide"); }
    }

我想&#39; [1]&#39;与LeafNo有关。如果是这样,设置SelectedSide = Sides [LeafNo]

<DataGridTextColumn Width="auto" MinWidth="50" Header="Notes" Binding="{Binding SelectedSide.Notes}"/>

答案 1 :(得分:0)

ASh提供了答案,只是重新发布,所以我可以标记为已回答

从右     {Binding Side [1] .Notes}

<强> WRONG     {Binding Side [1] / Notes}