使用 xaml 资源,但保留每个引用的唯一内容
我有几个列表框,并设置了组样式来制作组扩展器。我希望所有列表框都使用相同的样式信息来使它们全部成为扩展器,但我希望能够将标题的内容更改为每次使用该样式时自定义的内容。
有什么方法可以取出 的内容,但仍然编辑 的内容
目前,我正在使用与此类似的语法:
<ListBox x:Name="listBox1" ItemsSource="{Binding}" Height="571" Width="260">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Style="{StaticResource GroupBoxExpander}">
<Expander.Header>
<Grid Width="190" Background="Yellow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Count:" />
<TextBlock Grid.Column="1" Text="{Binding Path=ItemCount}" />
</Grid>
</Expander.Header>
<Expander.Content>
<ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:ItemControl1 />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
非常感谢任何帮助!
感谢您的阅读
I have several ListBoxes and have set up the group style to make the groups expanders. I want all of the ListBoxes to use the same style information to make them all expanders, but i want to be able to change the content of the header to be custom for each use of the style.
Is there any way i can take the out of the but still edit the contents of the
Currently, i am using a syntax similar to this:
<ListBox x:Name="listBox1" ItemsSource="{Binding}" Height="571" Width="260">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Style="{StaticResource GroupBoxExpander}">
<Expander.Header>
<Grid Width="190" Background="Yellow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Count:" />
<TextBlock Grid.Column="1" Text="{Binding Path=ItemCount}" />
</Grid>
</Expander.Header>
<Expander.Content>
<ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:ItemControl1 />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Any help greatly appreciated!
Thanks for reading
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GroupItem 是一个 ContentControl,因此您可以将相同的模板与不同的 ContentTemplate 一起使用。像这样分离您的样式:
ControlTemplate 中的 ContentPresenter 将实例化 DataTemplate,因此这将具有相同的外观,但您可以通过替换 DataTemplate 来自定义标题。您可以使用 ControlTemplate 创建基本样式,然后基于该样式创建重复使用 ControlTemplate 但具有不同 DataTemplate 的其他样式。
GroupItem is a ContentControl, so you could use the same Template with a different ContentTemplate. Separate your style out like this:
The ContentPresenter in the ControlTemplate will instantiate the DataTemplate, so this will have the same appearance, but you can customize the header by replacing the DataTemplate. You could create a base style with the ControlTemplate, and then create other styles based on that one that re-use the ControlTemplate but have a different DataTemplate.
您是否尝试过将模板(或样式)放在资源部分并设置 x:Shared="false" ?
Have you tried putting the Template (or the Style) in the Resources section and setting x:Shared="false" ?