我想在扩展程序内容模板中嵌入不同的usercontrol。
XAML:
<ListBox.ItemTemplate>
<DataTemplate>
<Expander IsExpanded="True" Header="{Binding}" Content="{Binding}">
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Status}"/>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<usercontrol:UserListGrid ></usercontrol:UserListGrid>
</Grid>
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
而不是将单个用户控件放在Expander.contenttemplate中。我的要求是在Expander.contenttemplate中显示不同的用户控件,请建议我如何实现这一点。
我可以实现与
相同的功能 MyCollection = new ObservableCollection<FrameworkElement>();
for (var ii = 0; ii < 5; ii++)
{
Expander dynamicExpander = new Expander();
dynamicExpander.Header = "New";
dynamicExpander.HorizontalAlignment = HorizontalAlignment.Left;
dynamicExpander.Background = new SolidColorBrush(Colors.Lavender);
dynamicExpander.IsExpanded = true;
if (ii == 1 || ii==0)
dynamicExpander.Content = GetGridContent("New");
if (ii == 2 || ii == 4)
dynamicExpander.Content = GetGridContent("New1");
MyCollection.Add(dynamicExpander);
}
private object GetGridContent(string header)
{
if (header == "New")
{
return new UserListGrid();
}
else
{
return new UserControlInWork.InWorkGrid();
}
}
我想用XAML实现同样的目标。
答案 0 :(得分:0)
使用Grid
或任何其他能够包含子元素的控件,并定义Grid.Row
和Grid.Column
附加属性:
<usercontrol:UserListGrid Grid.Row="0" />
<usercontrol:EquipemntListGrid Grid.Row="1" />