在按钮单击事件上显示或隐藏基于XAML的数据模板

时间:2014-09-24 19:10:07

标签: c# wpf xaml

我想在按钮点击事件中显示或隐藏基于Xaml的数据模板。我怎样才能做到这一点 ?数据模板与基于xml的数据源绑定使用c#WPF。帮助我这个。

3 个答案:

答案 0 :(得分:0)

您可以设置代码隐藏,使用其他数据模板更改其他样式。您必须为按钮(一个带有数据模板),第二个没有用于编写exaple两个样式。在"事件"你改变这种风格的方法。 在邮件xaml文件中:

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

在code-behid中:

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

在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

答案 1 :(得分:0)

Ok Awais Alvi我将所有代码都放在我的WPF应用程序中:

在App.xaml中:

    <Application x:Class="WpfApplication2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.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>
    </Application>

在MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="ControlButton" Style="{StaticResource buttonStyle1}" Content="Button" Click="EventButton"/>
    </Grid>
</Window>

在MainWindow.xaml.cs(代码隐藏)中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

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

尝试在新的WPF项目中复制我的代码。它必须工作。您已经写过,您是WPF中的最新版本,所以请注意,在我的代码中,您可能有另一个名称空间。

答案 2 :(得分:0)

ok mchrzanski这是我背后的代码

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        /// 
        private void EventButton(object sender, RoutedEventArgs e)
        {
            Style style = (Style)FindResource("buttonStyle2");
            ControlButton.Style = style;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}

加入app.xaml

<Application
    x:Class="App1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1">

    <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>
</Application>

和mainpage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

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