我可以使用触发器在控件之间进行切换吗

发布于 2024-08-19 00:10:49 字数 88 浏览 3 评论 0原文

我希望能够将我的列表视图“更改”为另一个控件。我正在考虑将控件的可见性设置为隐藏,然后单击按钮时更改可见性。我必须以编程方式执行此操作吗?或者我可以使用触发器吗?

I want to be able to "change" my listview into another control. I was thinking having the control's visibility set to hidden and when a button is clicked, change the visibility. Do I have to do this programatically? Or can I use a trigger?

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

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

发布评论

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

评论(3

假扮的天使 2024-08-26 00:10:49

您可以使用触发器来更改 ContentControl 的 Template 属性,这将包装您想要“可更改”的控件。检查一下:

将其添加到资源中:

    <ControlTemplate x:Key="BoxTemplate">
        <TextBox Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                        AncestorType={x:Type ContentControl}},Path=Content}" />
    </ControlTemplate>

    <ControlTemplate x:Key="BlockTemplate" >
        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                          AncestorType={x:Type ContentControl}},Path=Content}" />
    </ControlTemplate>

    <ControlTemplate x:Key="TestTemplate" >
        <StackPanel>
            <CheckBox x:Name="Switch" />
            <ContentControl x:Name="MyContent" Template="{StaticResource BoxTemplate}"
                            Content="Data is unique!" />
        </StackPanel>

        <ControlTemplate.Triggers>
            <Trigger SourceName="Switch" Property="IsChecked" Value="True">
                <Setter TargetName="MyContent" 
                        Property="Template" 
                        Value="{StaticResource BlockTemplate}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

使用另一个 ContentControl 对其进行测试:

    <ContentControl Template="{StaticResource TestTemplate}"/>  

我确信它可以进行优化,但应该会让您走上正轨。

You can use a trigger to change the Template property of a ContentControl, which will wrap the control you want to be "changeable". Check this:

Add this to Resources:

    <ControlTemplate x:Key="BoxTemplate">
        <TextBox Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                        AncestorType={x:Type ContentControl}},Path=Content}" />
    </ControlTemplate>

    <ControlTemplate x:Key="BlockTemplate" >
        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                          AncestorType={x:Type ContentControl}},Path=Content}" />
    </ControlTemplate>

    <ControlTemplate x:Key="TestTemplate" >
        <StackPanel>
            <CheckBox x:Name="Switch" />
            <ContentControl x:Name="MyContent" Template="{StaticResource BoxTemplate}"
                            Content="Data is unique!" />
        </StackPanel>

        <ControlTemplate.Triggers>
            <Trigger SourceName="Switch" Property="IsChecked" Value="True">
                <Setter TargetName="MyContent" 
                        Property="Template" 
                        Value="{StaticResource BlockTemplate}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

Test it using another ContentControl:

    <ContentControl Template="{StaticResource TestTemplate}"/>  

I'm sure it could be optimized, but should put you on the track.

烟凡古楼 2024-08-26 00:10:49

您可以使用故事板和/或触发器来执行此操作,没问题。只需对要更改的元素的可见性属性进行动画处理即可。

You can use a Storyboard and/or trigger to do this, no problem. Just animate the Visibility properties on the elements you want to change.

写给空气的情书 2024-08-26 00:10:49

要添加 Muad'Dib 的答案,另一种方法是将控件堆叠在一起,然后在触发器中来回淡出不透明度。您还可以使用 VSM 来执行此操作(创建 ListboxVisible 状态和 ListboxHidden 状态,或任何在语义上更有意义的名称)

To add to Muad'Dib's answer, another way you can do it is to stack the controls on top of each other, then fade the Opacity back and forth in the trigger. You can also do this with VSM (Create a ListboxVisible state and a ListboxHidden state, or whatever name makes more sense semantically)

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