Dictionary <int,observablecollection <entity =“”>&gt;捆绑

时间:2016-09-11 08:30:43

标签: c# wpf binding

在我的应用程序中,我获得了一个字典,其中int为关键字,ObservableCollection为值:

Dictionary<int, ObservableCollection<Entity>> SourceDict { get; set; }

现在我想将词典绑定到Datagrid,其中应显示此ObservableCollections的所有Dictionary

如果我这样绑定它:

ItemsSource="{Binding SourceDict}"

我得到一个空的Datagrid。有没有办法做到这一点或在绑定之前我应该​​将Dictionary转换为ObservableCollection

1 个答案:

答案 0 :(得分:0)

 <Grid>
    <ListView HorizontalAlignment="Center" Width="525" Margin="0,42,0,52" ItemsSource="{Binding SourceList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <ListView.View>
            <GridView x:Name="gv_Listview">
                <GridViewColumn Header="Age">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Black" BorderThickness=".3" Margin="-6,-3">
                                <TextBlock TextAlignment="Center" Text="{Binding Age}" Height="30" Margin="6,3" Width="90"/>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Black" BorderThickness=".3" Margin="-6,-3">
                                <TextBlock x:Name="txtAriza" TextAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" Height="30" Margin="6,3" Width="112"/>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="Surname">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Black" BorderThickness=".3 .3 .0 .3" Margin="-6,-3">
                                <TextBlock TextAlignment="Center" Text="{Binding Surname}" Height="30" Margin="6,3" Width="60"/>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>
</Grid>

这是TestEntity

public class TestEntity : INotifyPropertyChanged
{
    private string _name ;
    private string _surname;
    private int _age ;

    public string Name
    {
      get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    public int Age
    {
        get { return _age; }
        set { _age = value; OnPropertyChanged("Age"); }
    }

    public string Surname
    {
      get { return _surname; }
        set { _surname = value; OnPropertyChanged("Surname"); }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

和DataContext

 private List<ObservableCollection<TestEntity>> _source = new List<ObservableCollection<TestEntity>>();
    public List<ObservableCollection<TestEntity>> SourceList
    {
        get { return _source; }
        set { _source = value; }
    }
    public DataContext()
    {
        this.SourceList.Add(new ObservableCollection<TestEntity> { new TestEntity { Age = 21 , Name = "Chris" , Surname = "James"}});
        this.SourceList.Add(new ObservableCollection<TestEntity> { new TestEntity { Age = 23, Name = "Jin", Surname = "Evelin" } });
        this.SourceList.Add(new ObservableCollection<TestEntity> { new TestEntity { Age = 33, Name = "Ryan", Surname = "Thrusty" } });
        this.SourceList.Add(new ObservableCollection<TestEntity> { new TestEntity { Age = 34, Name = "Martin", Surname = "Riggs" } });
    }

希望这有帮助。