【问题标题】:wpf mvvm displaying observable collection in datagridwpf mvvm 在数据网格中显示可观察的集合
【发布时间】:2013-11-04 13:19:03
【问题描述】:

我需要在DataGrid中显示一个对象的所有信息

ItemsCollectionAItemsCollectionBItemsCollectionC​​trong>都是ObservableCollections

我有一个数据网格:

<DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" />

这会以网格格式显示所有属性,但 ItemsCollectionBItemsCollectionC​​trong> 显示为(集合)。

我怎样才能让 ItemsCollectionBItemsCollectionC​​trong> 向下和向外扩展网格以显示它们的属性

【问题讨论】:

  • 这是因为它不知道如何显示这些集合。它基本上是在您的ItemsCollectionBItemsCollectionC 上调用.ToString()。我们需要更多信息来回答您的问题。正如@geedubb 所问,你想为这些显示什么?
  • 现在它告诉我有一个集合,我想要集合的每个项目中的所有值,而不是显示集合的每个项目中存在的名为 Name 或 Value 的属性(为清楚起见进行编辑)
  • @Oliver 你自动生成列吗?因为如果不是这样,这相当容易
  • 只是一个更新,说我还在看这个,可能会得到一个解决方案,尽管它不再使用数据网格,因为问题是显示集合

标签: c# wpf mvvm datagrid


【解决方案1】:

如果您可以指定列而不是自动生成它们,这将非常容易。

这是一个例子:

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding EmployeeName}"/>
        <!-- Displays the items of the first collection-->
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Dogs}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <!-- Displays the items of the second collection-->
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Cats}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

视图模型:

public class MainWindowViewModel : NotificationObject
{
    public MainWindowViewModel()
    {
        Employees = new ObservableCollection<Employee>
        {
            new Employee { EmployeeName = "Steven"},
            new Employee { EmployeeName = "Josh"},
        };
    }

    public ObservableCollection<Employee> Employees { get; set; }
}

型号:

public class Employee
{
    public Employee()
    {
        Dogs = new ObservableCollection<Dog>
        {
            new Dog { Gender = 'M'},
            new Dog { Gender = 'F'},
        };
        Cats = new ObservableCollection<Cat>
        {
            new Cat { Name = "Mitzy" , Kind = "Street Cat"},
            new Cat { Name = "Mitzy" , Kind = "House Cat"}
        };
    }

    public string EmployeeName { get; set; }

    public ObservableCollection<Dog> Dogs { get; set; }

    public ObservableCollection<Cat> Cats { get; set; }
}

public class Dog
{
    public char Gender { get; set; }

    public override string ToString()
    {
        return "Dog is a '" + Gender + "'";
    }
}

public class Cat
{
    public string Name { get; set; }

    public string Kind { get; set; }

    public override string ToString()
    {
        return "Cat name is " + Name + " and it is a " + Kind;
    }
}

ItemsCollectionA 视为EmployeesItemsCollectionBItemsCollectionC 作为DogsCats。它仍然使用ToString 来显示我覆盖的DogsCats 对象,但是您可以简单地将DataTemplate 设置为列中的列表框来决定如何显示您的模型。还要注意DataGrid 上的AutoGenerateColumns="False",以避免创建两次列。

希望对你有帮助

【讨论】:

    【解决方案2】:

    一个DataGrid只能管理一个项目源。一切都是基于行的,列并不那么智能。

    两种方式:

    • 将您的数据组合到具有两组字段的新对象中(这将是最简单的)。
    • 并排同步 2 个数据网格。

    【讨论】:

      【解决方案3】:

      好吧,似乎 datagrid 完全是我在这里需要的错误。我制作了一个列表框的堆栈面板,其中 itemssource 设置为每个绑定,并且显示正常

      <StackPanel Background="white" HorizontalAlignment="Stretch"  Margin="0">
              <ListBox Background="white" x:Name="BetsListBox"  VerticalAlignment="Stretch" BorderThickness="0" 
                           ItemsSource="{Binding Path=ItemsCollectionA}" Margin="0" Width="Auto" HorizontalAlignment="Stretch" >
                  <ListBox.Resources>
                      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F0F0F0"/>
                      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#F0F0F0"/>
                      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                  </ListBox.Resources>
                  <ListBox.ItemTemplate>
                      <DataTemplate>
                          <StackPanel >
                              <ListBox ItemsSource="{Binding Path=ItemsCollectionB}">
                                  <ListBox.ItemTemplate>
                                      <DataTemplate>
                                          <StackPanel Orientation="Horizontal">
                                              <TextBlockText="{Binding Path=varA}" />
                                              <TextBlockText="{Binding Path=varB}" />
                                              <TextBlockText="{Binding Path=varC}" />
                                          </StackPanel>
                                      </DataTemplate>
                                  </ListBox.ItemTemplate>
                              </ListBox>
                              <ListBox BorderThickness="0" ItemsSource="{Binding Path=ItemsCollectionC}" >
                                  <ListBox.ItemTemplate>
                                      <DataTemplate>
                                          <StackPanel  Orientation="Horizontal" >
                                              <TextBlock Text="{Binding Path=VarA}" ToolTip="{Binding Name}"/>
                                              <TextBlock Text="{Binding Path=VarB}" ToolTip="{Binding Name}"/>
                                              <TextBlock Text="{Binding Path=VarC}" ToolTip="{Binding Name}"/>
                                          </StackPanel>
                                      </DataTemplate>
                                  </ListBox.ItemTemplate>
                              </ListBox >
                          </StackPanel>
                      </DataTemplate>
                  </ListBox.ItemTemplate>
              </ListBox>
          </StackPanel>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-20
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        • 2019-06-02
        • 1970-01-01
        相关资源
        最近更新 更多