【发布时间】:2012-01-04 10:17:00
【问题描述】:
我有一个启用了 rowdetails 属性的 WPF 工具包网格。
我的rowdetails模板如下
<DataTemplate x:Key="rowDetailsTemplate">
<Border BorderBrush="Black" BorderThickness="1" Background="#BFEFF2F5">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<ListBox Margin="100, 0, 0, 0" ItemsSource ="{Binding Path=GridSubItems}">
</ListBox>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
GridSubItems 定义在后面的代码中:
public class GridDataSource
{
private List<GridItemData> _GridItems = new List<GridItemData>();
private List<GridItemData> _GridSubItems = new List<GridItemData>();
public List<GridItemData> GridItems
{
get
{
return _GridItems;
}
set
{
_GridItems = value;
}
}
public List<GridItemData> GridSubItems
{
get
{
return _GridSubItems;
}
set
{
_GridSubItems = value;
}
}
}
及其在 XAML 中定义的数据:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:myapp="clr-namespace:MyControlsDemo.DemoResources"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<myapp:GridDataSource x:Key="dgs">
<myapp:GridDataSource.GridItems>
<myapp:GridItemData Name="Item1" Type="Numeric" Unit="MHz" Key="i1" />
<myapp:GridItemData Name="Item2" Type="List" Unit="enum" Key="i2" />
<myapp:GridItemData Name="Item3" Type="Text" Unit="text" Key="i3" />
</myapp:GridDataSource.GridItems>
<myapp:GridDataSource.GridSubItems>
<myapp:GridItemData Name="SubItem1" Type="Numeric" Unit="MHz" Key="si1" />
<myapp:GridItemData Name="SubItem2" Type="Numeric" Unit="MHz" Key="si2" />
<myapp:GridItemData Name="SubItem3" Type="Numeric" Unit="MHz" Key="si3" />
</myapp:GridDataSource.GridSubItems>
</myapp:GridDataSource>
这就是这个数据模板的使用方式:
<ContentControl Grid.Column="2">
<centerpanel:GenericParametersGrid DataContext="{StaticResource dgs}"
ItemsSource="{Binding Path=GridItems}"
RowDetailsTemplate="{StaticResource ResourceKey=rowDetailsTemplate}"
Style="{StaticResource ResourceKey=centerPanelHierParameterGridStyle}"/>
</ContentControl>
但是我在我的应用程序中看不到子项,列表框显示为空。 如果我直接在数据模板中定义子项,我可以看到它们,所以我猜问题出在我的数据绑定中,但对于我的生活,我看不出有什么问题。我应该承认我对 WPF 的体验非常短暂。
非常感谢您的任何建议。 浮动
【问题讨论】:
-
向我们展示您如何将该数据模板应用到 XAML 中的控件
-
查看“输出”窗口是否有错误。我通常会在那里找到问题。
-
@Nagg - 查看我编辑的问题
-
@Lucian - 输出窗口中没有任何可疑之处
标签: c# wpf xaml data-binding