C# wpf mvvm在datagrid中显示可观察的集合
我需要在C# wpf mvvm在datagrid中显示可观察的集合,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,我需要在DataGrid 项目集合A、项目集合B和项目集合C都是可观察的集合 我有一个数据网格: <DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" /> 这将以网格格式显示所有属性,但ItemsCollectionB和ItemsCollectionC显示为(集合) 如何获取ItemsCollectionB和ItemsCollectionC以向下和向外扩展网格以显示其属
DataGrid
项目集合A、项目集合B和项目集合C都是可观察的集合
我有一个数据网格:
<DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" />
这将以网格格式显示所有属性,但ItemsCollectionB和ItemsCollectionC显示为(集合)
如何获取ItemsCollectionB和ItemsCollectionC以向下和向外扩展网格以显示其属性ADataGrid只能管理一个项目源。所有内容都是基于行的,列也没有那么智能 两种方式:
- 要将数据合并到包含两组字段的新对象中(最简单)
- 并排同步2个数据网格
<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; }
}
公共类MainWindowViewModel:NotificationObject
{
公共主窗口视图模型()
{
员工=新的可观察收集
{
新员工{EmployeeName=“Steven”},
新员工{EmployeeName=“Josh”},
};
}
公共ObservableCollection雇员{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;
}
}
公共类员工
{
公职人员()
{
Dogs=新的可观察收集
{
新狗{性别='M'},
新狗{性别='F'},
};
Cats=新的可观察收集
{
新猫{Name=“Mitzy”,Kind=“Street Cat”},
新猫{Name=“Mitzy”,Kind=“家猫”}
};
}
公共字符串EmployeeName{get;set;}
公共可观察收集狗{get;set;}
公共可观测集合猫{get;set;}
}
公家犬
{
公共字符{get;set;}
公共重写字符串ToString()
{
返回“狗是一个“+”性别“+”;
}
}
公猫
{
公共字符串名称{get;set;}
公共字符串种类{get;set;}
公共重写字符串ToString()
{
return“猫名为”+名+”,为“+类;
}
}
将ItemsCollectionA
视为Employees
和ItemsCollectionB
和ItemsCollectionC
视为Dogs
和Cats
。它仍然使用ToString
来显示我覆盖的Dogs
和Cats
对象,但您只需在列中的列表框中设置DataTemplate
,即可决定如何显示模型。还要注意DataGrid
上的AutoGenerateColumns=“False”
,以避免两次创建列
希望这能有所帮助,好吧,看来datagrid完全不是我需要的东西。我制作了一个listbox的stackpanel,每个绑定都设置了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>
这是因为它不知道如何显示这些集合。它基本上是在ItemsCollectionB
和ItemsCollectionC
上调用.ToString()
。我们需要更多的信息来回答你的问题。正如@geedubb所问的,您希望为这些显示什么?现在它告诉我有一个集合,我希望集合中每个项目中的所有值都显示出来,即显示集合中每个项目中存在的名为Name或Value的属性(为清晰起见进行编辑)@Oliver您必须自动生成列吗?因为如果不是的话,这是相当容易的,只是一个更新,我仍然在看这个,可能会得到一个解决方案,尽管它不再使用数据网格,因为问题是显示集合