[英]Datagrid ObservableCollection for one row wpf
I want to populate a datagrid with itemsource={Binding Model}. 我想用itemsource = {Binding Model}填充一个数据网格。 This is not working out.
这没有解决。 It seems as the datagrid does not understand how to display these properties.
好像datagrid不了解如何显示这些属性。
An easy but silly workaround works great: 一个简单但愚蠢的解决方法非常有效:
In viewmodel: 在视图模型中:
Props= new ObservableCollection<MonitoringBinaryModel>();
_Model = new MonitoringBinaryModel(name);
Props.Add(_Model);
Then in xaml 然后在xaml中
itemsource={Binding Props}
Seems silly to create an observablecollection when its only suppose to hold one item. 当它只想容纳一个项目时,似乎愚蠢地创建了一个可观察的集合。 Is there a better way to make any type of instance observable?
是否有更好的方法可观察任何类型的实例?
DataGrid is designed to display a collection of objects of same type. DataGrid旨在显示相同类型的对象的集合。 Collection is a must.
收藏是必须的。 If you want DataGrid to show a content of your model, you need to obey former's design, by either using ObservableCollection or implementing a bunch of interfaces which would allow your viewmodel's properties to be retrieved in 'collection way'.
如果您想让DataGrid显示模型的内容,则需要使用ObservableCollection或实现一堆接口来遵循前者的设计,这些接口将允许以“集合方式”检索viewmodel的属性。
I used to have a bunch of models implementing ITypedList interface back in Windows Forms time - it wasn't a simple exercise to say the truth, so if I were you I'd rather go for either way: 我曾经有很多模型可以在Windows Forms的时间里实现ITypedList接口-讲真话并不是一个简单的练习,所以如果我是我,我宁愿选择这两种方法:
Replace data grid with container grid plus a number of direct bindings, like this: 将数据网格替换为容器网格以及许多直接绑定,如下所示:
<Grid> <TextBlock Grid.Column="2" Grid.Row="0" Text="Prop1"/> ... <TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding Prop1}"/>
Well ItemsSource
property is of type IEnumarable
so until your MonitoringBinaryModel
implement IEnumerable
binding will not work. 好
ItemsSource
属性的类型为IEnumarable
因此直到您的MonitoringBinaryModel
实现IEnumerable
绑定都无法使用。
Again because ItemsSource
is IEnumerable
you should provide IEnumerable
as binding source. 同样,由于
ItemsSource
是IEnumerable
您应该提供IEnumerable
作为绑定源。 So there is no need to make yout Props
as ObservableCollection
. 因此,无需将
Props
为ObservableCollection
。 You can use ordinary List<>
, or anything implementing IEnumerable
with your single MonitoringBinaryModel
: 您可以使用普通的
List<>
或任何通过单个MonitoringBinaryModel
实现IEnumerable
东西:
_Model = new MonitoringBinaryModel(name);
Props = new List<MonitoringBinaryModel> { _Model };
Other option is to use CompositeCollection
inside your XAML: 另一个选择是在XAML中使用
CompositeCollection
:
<DataGrid.ItemsSource>
<CompositeCollection>
<Binding Path="_Model"/>
</CompositeCollection>
</DataGrid.ItemsSource>
reusable wrapper via converter: 通过转换器可重复使用的包装器:
public class ItemsSourceConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// doesn't allow to add new rows in DataGrid
return Enumerable.Repeat(value, 1);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
usage in xaml 在XAML中的用法
add converter to resourses: 将转换器添加到资源:
<Window.Resources>
<wpfApplication1:ItemsSourceConverter x:Key="ItemWrapper"/>
</Window.Resources>
and use converter resourse with binding 并结合使用转换器资源
<DataGrid ItemsSource="{Binding Path=Model, Converter={StaticResource ItemWrapper}}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}"/>
</DataGrid.Columns>
</DataGrid>
or 要么
<ItemsControl ItemsSource="{Binding Path=Model, Converter={StaticResource ItemWrapper}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.