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 .
Asked
Active
Viewed 1,700 times
-1
-
have you looked into the BooleanToVisibility converter? essentially you can bind the visibility of your data template to a boolean value that is set through the event of the button click. I'm not sure if this is what you want but that is one way to toggle visibility through a button click. – Matthew Sep 24 '14 at 19:19
-
actually this is really what i need to do . i need to toggle a plus minus button and want to show data template on plus click ,, and hide it on minus click . – Awais Alvi Sep 24 '14 at 19:27
3 Answers
0
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"/>
In code-behid:
private void EventButton(object sender, RoutedEventArgs e)
{
Style style = (Style)FindResource("buttonStyle2");
ControlButton.Style = style;
}
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>
When you clicked button you change style from buttonStyl1 to buttonStyle2

wsc
- 50
- 1
- 8
-
mchrzanski thanks .. just tell me how can i set two styles for a button .. i am a beginner please help me . – Awais Alvi Sep 24 '14 at 19:23
-
actually this is really what i need to do . i need to toggle a plus minus button and want to show data template on plus click ,, and hide it on minus click . – Awais Alvi Sep 24 '14 at 19:28
-
-
Windows.UI.Xaml.FrameworkElement.Style' is a 'property' but is used like a 'type' – Awais Alvi Sep 24 '14 at 20:14
-
Show me your code. I've tested that code what I posted and it's working fine. I suppose you're doing something wrong. I think that you should first copy my code and try this. When you discover that it works fine, then you should trying editing this and puting that code what you want. – wsc Sep 24 '14 at 20:20
-
i did every thing as u said . just added styles in app.xaml in between Application.Resources tags . added in my xaml page and on EventButton m doing same . – Awais Alvi Sep 24 '14 at 20:27
0
Ok Awais Alvi I'm putting all my code from my WPF App so:
In 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>
In 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>
In MainWindow.xaml.cs (code-behind):
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;
}
}
}
Try copy my code in new WPF project. It must working. You've wrote that you're newest in WPF so please note that in my code, you may have another namespaces.

wsc
- 50
- 1
- 8
-
-
-
Alwais Alvi FindResource(string) method is not present in Windows Store Applications. I don't know what is it equivalent, so you should search in google or try programing desktop applications. – wsc Sep 24 '14 at 21:07
0
ok mchrzanski here is my code behind
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)
{
}
}
}
adn the 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>
and 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>

Awais Alvi
- 29
- 1
- 8
-
and the error is The name 'FindResource' does not exist in the current context D:\App1\App1\MainPage.xaml.cs 37 34 App1 – Awais Alvi Sep 24 '14 at 20:54