[英]Bind datagrid to an ObservableCollection list
I am new with WPF and I would like to bind from xaml to an ObservableCollection. 我是WPF的新手,我想将xaml绑定到ObservableCollection。 Currently I am doing like this:
目前我正在这样做:
in xaml: 在xaml中:
<Grid Grid.Column="1">
<local:ProcessingStep DataContext="{Binding ProcessingSteps[0]}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
<Grid Grid.Column="2">
<local:ProcessingStep DataContext="{Binding ProcessingSteps[1]}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
<Grid Grid.Column="3">
<local:ProcessingStep DataContext="{Binding ProcessingSteps[2]}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
in cs: 在cs中:
public ObservableCollection<ProcessingStepView> ProcessingSteps
{
get
{
return m_ProcessingSteps ??
(m_ProcessingSteps = new ObservableCollection<ProcessingStepViewl>
{
new ProcessingStepView("Step1"),
new ProcessingStepView("Step2"),
new ProcessingStepView("Step3")
});
}
}
How can I bind the list directly to wpf? 如何将列表直接绑定到wpf? eg: if I have 100 steps, it is not nice to do one by one.
例如:如果我有100个步骤,那么一步一步做就不好了。
You can use ItemsControl
with ItemTemplate
and bind your list: 您可以将
ItemsControl
与ItemTemplate
结合使用并绑定列表:
<ItemsControl ItemsSource="{Binding ProcessingSteps}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ProcessingStep />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ItemsControl
is basically just a repeater, without selection, that will repeat ItemTemplate
as many times as many items there are in ProcessingSteps
and place them in whichever panel you choose. ItemsControl
基本上只是一个转发器,无需选择,它将重复ItemTemplate
在ProcessingSteps
存在的项目数次,并将它们放置在您选择的任何面板中。 In this case horizontal StackPanel
在这种情况下,水平
StackPanel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.