[英]binding a control with a observable collection
In xaml, how do I display an ObservableCollection in a expander control with a grid when you expand it? 在xaml中,展开时如何在带有网格的扩展器控件中显示ObservableCollection?
public class BoundPoco
(
public string ExpanderText { set; get; }
public ObservableCollection<DisplayDetail> { set; get; }
)
public class DisplayDetail
{
public string FirstDisplayColumn { set; get; }
public string SecondDisplayColumn; { set; get; }
}
You should use an items control within the expander control to achieve this. 您应该在扩展器控件中使用项目控件来实现此目的。
Firstly sort your code behind errors out - you had a stray semicolon on "SecondDisplayColumn": 首先,将您的代码归类为错误-您在“ SecondDisplayColumn”上使用了分号:
public string SecondDisplayColumn { set; get; }
... and you had no name on the main observable collection property! ...而且您在可观察的主要收藏属性中没有名字!
public ObservableCollection<DisplayDetail> DisplayDetails { set; get; }
Then, if you're not using dependency properties or INotifyPropertyChanged - create your XAML to display the data like so (with a name 'items'): 然后,如果您不使用依赖项属性或INotifyPropertyChanged,请创建XAML来显示数据(名称为“ items”),如下所示:
<Expander Name="myExpander" Background="Tan"
HorizontalAlignment="Left" Header="My Expander"
ExpandDirection="Down" IsExpanded="True" Width="Auto">
<ItemsControl Name="items">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding FirstDisplayColumn}"/>
<TextBlock Grid.Column="1" Text="{Binding SecondDisplayColumn}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
Then you can populate the observable collection and set the items source in codebehind: 然后,您可以填充可观察的集合,并在代码后面设置项目源:
DisplayDetails.Add(new DisplayDetail() { FirstDisplayColumn = "FieldName1", SecondDisplayColumn = "FieldValue1" });
DisplayDetails.Add(new DisplayDetail() { FirstDisplayColumn = "FieldName2", SecondDisplayColumn = "FieldValue2" });
DisplayDetails.Add(new DisplayDetail() { FirstDisplayColumn = "FieldName3", SecondDisplayColumn = "FieldValue3" });
items.ItemsSource = DisplayDetails;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.