XAML:为扩展器中表示的 ViewModel 创建数据模板

发布于 2024-11-10 09:14:17 字数 231 浏览 0 评论 0原文

假设我有一个类似于 Expander 的自定义控件,并将在该扩展器中显示多种类型的对象。我想为每种类型的对象定义一个 DataTemplate。

现在我想在未展开时显示特定信息,在展开时显示其他信息。

通常,使用扩展器,它仅显示绑定到 Header 属性的内容。

我可以以某种方式在 DataTemplate 中为每个视图定义两个区域吗?

也许还有其他一些绝妙的方法可以做到这一点吗?

Let´s say I have a custom control similar to Expander and will be showing multiple types of objects in that expander. I want to define a DataTemplate for each type of object.

Now I want to show specific information when it's not expanded and something else when it is.

Normally with expander it only shows whatever is bound to the Header property.

Can I somehow define two areas in the DataTemplate for each view?

Is there perhaps some other brilliant way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

维持三分热 2024-11-17 09:14:17

试试这个:

<DataTemplate x:Key="ExpanderItemDataTemplate">
        <Grid x:Name="LayoutRoot">
            <Grid x:Name="ExpandedContent" />
            <Grid x:Name="CollapsedContent" />
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType=local:YourCustomControl}}"
                         Value="True">
                <Setter Property="Visibility"
                        TargetName="ExpandedContent"
                        Value="Visible" />
                <Setter Property="Visibility"
                        TargetName="CollapsedContent"
                        Value="Collapse" />
            </DataTrigger>
            <DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType=local:YourCustomControl}}"
                         Value="False">
                <Setter Property="Visibility"
                        TargetName="ExpandedContent"
                        Value="Collapse" />
                <Setter Property="Visibility"
                        TargetName="CollapsedContent"
                        Value="Visible" />
            </DataTrigger>
        </DataTemplate.Triggers>

Try this:

<DataTemplate x:Key="ExpanderItemDataTemplate">
        <Grid x:Name="LayoutRoot">
            <Grid x:Name="ExpandedContent" />
            <Grid x:Name="CollapsedContent" />
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType=local:YourCustomControl}}"
                         Value="True">
                <Setter Property="Visibility"
                        TargetName="ExpandedContent"
                        Value="Visible" />
                <Setter Property="Visibility"
                        TargetName="CollapsedContent"
                        Value="Collapse" />
            </DataTrigger>
            <DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType=local:YourCustomControl}}"
                         Value="False">
                <Setter Property="Visibility"
                        TargetName="ExpandedContent"
                        Value="Collapse" />
                <Setter Property="Visibility"
                        TargetName="CollapsedContent"
                        Value="Visible" />
            </DataTrigger>
        </DataTemplate.Triggers>

┼── 2024-11-17 09:14:17

您是否考虑过简单地使用 TabControl
例如,您可以添加两个选项卡并设置它们的样式。这是选项卡样式的代码:

    <Style TargetType="{x:Type TabPanel}">
        <!--Whatever you need for tab position (here center) -->
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>

           <ControlTemplate x:Key="TabItemTemplate" TargetType="{x:Type TabItem}">
                <!-- Place whatever control you want for design (grid, dockpanel... -->
                <!-- And then the triggers you'd need for, here, color if selected or not, as an example -->
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Blue" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>

是我思考它的自然方式。
您可以扩展此代码,例如,在单击唯一可见的选项卡时添加一个更改所选选项卡的触发器。
无论如何,您所描述的行为似乎更适合 TabControl 而不是 Expander

Have you considered simply use a TabControl ?
You could for example add two tabs and style them. Here is the code for tab styling:

    <Style TargetType="{x:Type TabPanel}">
        <!--Whatever you need for tab position (here center) -->
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>

and

           <ControlTemplate x:Key="TabItemTemplate" TargetType="{x:Type TabItem}">
                <!-- Place whatever control you want for design (grid, dockpanel... -->
                <!-- And then the triggers you'd need for, here, color if selected or not, as an example -->
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Blue" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>

This is the natural way I'd think about it.
You coult extend this code to, for example, add a trigger changing the selected tab when you click on the only visible tab.
Anyway, the behavior you're describing seems more to fit with a TabControl than an Expander

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