在按钮单击事件上显示或隐藏基于 XAML 的数据模板 [英] Show or Hide XAML Based Data template on button click event

查看:33
本文介绍了在按钮单击事件上显示或隐藏基于 XAML 的数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按钮单击事件上显示或隐藏基于 Xaml 的数据模板.我怎样才能做到这一点 ?数据模板与基于 xml 的数据源绑定,使用 c# WPF.帮我解决这个问题.

i want to show or hide a Xaml based Data template on a button click event . How can i do that ? the data template is bounded with xml based data source Using c# WPF. Help me about this .

推荐答案

您可以在代码隐藏中设置使用其他数据模板更改另一种样式.例如,您必须为按钮编写两种样式(一种带有数据模板),第二种没有.在事件"方法中,您可以更改此样式.在邮件 xaml 文件中:

You can set in code-behind changing another style with other Data template. You must write for exaple two styles for button (one with data template), second without. And in the "event" method you changing this styles. In mail xaml file:

<Button Name="ControlButton" Style="{StaticResource buttonStyle1}" Content="Button" Click="EventButton"/>

在代码隐藏中:

    private void EventButton(object sender, RoutedEventArgs e)
    {
        Style style = (Style)FindResource("buttonStyle2");
        ControlButton.Style = style;
    }

在 App.xaml 中:

In App.xaml:

    <Application.Resources>
    <Style TargetType="Button" x:Key="buttonStyle1">
        <Setter Property="Foreground" Value="Yellow" />
    </Style>

    <Style TargetType="Button" x:Key="buttonStyle2">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>

当您单击按钮时,您将样式从 buttonStyl1 更改为 buttonStyle2

When you clicked button you change style from buttonStyl1 to buttonStyle2

这篇关于在按钮单击事件上显示或隐藏基于 XAML 的数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆