在按钮单击事件上显示或隐藏基于 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