使用 xaml 资源,但保留每个引用的唯一内容

发布于 2024-09-18 13:53:21 字数 2419 浏览 5 评论 0原文

我有几个列表框,并设置了组样式来制作组扩展器。我希望所有列表框都使用相同的样式信息来使它们全部成为扩展器,但我希望能够将标题的内容更改为每次使用该样式时自定义的内容。

有什么方法可以取出 的内容,但仍然编辑 的内容

目前,我正在使用与此类似的语法:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2024-09-25 13:53:21

GroupItem 是一个 ContentControl,因此您可以将相同的模板与不同的 ContentTemplate 一起使用。像这样分离您的样式:

<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="True" >
                    <Expander.Header>
                        <ContentPresenter/>
                    </Expander.Header>
                    <Expander.Content>
                        <ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
                    </Expander.Content>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <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>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ControlTemplate 中的 ContentPresenter 将实例化 DataTemplate,因此这将具有相同的外观,但您可以通过替换 DataTemplate 来自定义标题。您可以使用 ControlTemplate 创建基本样式,然后基于该样式创建重复使用 ControlTemplate 但具有不同 DataTemplate 的其他样式。

<Style TargetType="{x:Type GroupItem}"
       x:Key="BaseGroupStyle">
    <Setter Property="Template" .../>
</Style>
<Style TargetType="{x:Type GroupItem}"
       BasedOn="{StaticResource BaseGroupStyle}"
       x:Key="CountGroupStyle">
    <Setter Property="ContentTemplate" .../>
</Style>

GroupItem is a ContentControl, so you could use the same Template with a different ContentTemplate. Separate your style out like this:

<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="True" >
                    <Expander.Header>
                        <ContentPresenter/>
                    </Expander.Header>
                    <Expander.Content>
                        <ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
                    </Expander.Content>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <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>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

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.

<Style TargetType="{x:Type GroupItem}"
       x:Key="BaseGroupStyle">
    <Setter Property="Template" .../>
</Style>
<Style TargetType="{x:Type GroupItem}"
       BasedOn="{StaticResource BaseGroupStyle}"
       x:Key="CountGroupStyle">
    <Setter Property="ContentTemplate" .../>
</Style>
等待我真够勒 2024-09-25 13:53:21

您是否尝试过将模板(或样式)放在资源部分并设置 x:Shared="false" ?

Have you tried putting the Template (or the Style) in the Resources section and setting x:Shared="false" ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文