我的用户控件定义如下:
XAML:
<UserControl>
<Grid x:Name="LayoutRoot">
<TabControl x:Name="TabControl" ItemsSource="{Binding Products}" >
<TabControl.ContentTemplate>
<DataTemplate>
<Grid>
<ListView ItemsSource="{Binding Features}" Name="FeaturesListView">
<ListView.View>
<GridView>
<GridViewColumn x:Name="FeatureHeader" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn x:Name="CounterHeader" DisplayMemberBinding="{Binding Counter}" />
<GridViewColumn x:Name="ExpireDateHeader" DisplayMemberBinding="{Binding ExpireDate}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
代码背后的代码:
public partial class LicenseInfoControl : UserControl
{
public LicenseInfoControl(ShowableLicense license)
{
InitializeComponent();
DataContext = license;
}
}
因此,如果我为一个窗口使用一个用户控件:
<Window>
<Grid>
<local:LicenseInfoControl x:Name="LicenseInfoControl"/>
</Grid>
</Window>
效果很好。但如果我使用其中两个:
<Window>
<Grid>
<local:LicenseInfoControl x:Name="FileLicenseInfoControl"/>
<local:LicenseInfoControl x:Name="SamLicenseInfoControl"/>
</Grid>
</Window>
两个ListView都是空的。我做错了什么?
答案 0 :(得分:0)
绑定效果很好。问题是如果TabControl有一个TabItem - 当窗口加载并且数据暴露给它时它会被选中。但是如果TabControl有两个TabItems - 没有一个获得选择,并且有一种感觉,绑定没有奏效。但是,如果您选择其中一个选项卡项 - 数据会立即显示。 因此,决定是在具有两个用户控件的窗口的构造函数中设置SelectedIndexes(在我的情况下为LicenseInfoControl):
FileLicenseInfoControl.ProductTabControl.SelectedIndex = 0;
SamLicenseInfoControl.ProductTabControl.SelectedIndex = 0;