I'm attempting to use a data-trigger on a style to change a property.
In compliance with the "Minimal, Complete and Verifiable Example" requirements...
To reproduce, first create a WPF application in Visual Studio.
Within the App.xaml.cs :
using System.ComponentModel;
using System.Windows;
namespace Foo{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, INotifyPropertyChanged {
private bool _clicked;
public bool Clicked {
get { return this._clicked; }
set {
this._clicked = value;
this.PropertyChanged?.Invoke(
this, new PropertyChangedEventArgs( "Clicked" ) );
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Within the MainWindow.xaml :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lib="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Foo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d" x:Class="Foo.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<lib:Boolean x:Key="True">True</lib:Boolean>
</Window.Resources>
<Grid>
<Button x:Name="button" Click="button_Click">
<Viewbox>
<TextBlock Text="Unclicked">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger
Binding="{Binding
Clicked,
Source={x:Static Application.Current}}"
Value="{StaticResource True}">
<Setter Property="Text" Value="Clicked" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Viewbox>
</Button>
</Grid>
</Window>
Within the MainWindow.xaml.cs -
using System.Windows;
namespace Foo{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow( ) {
InitializeComponent( );
}
private void button_Click( object sender, RoutedEventArgs e ) {
( Application.Current as App ).Clicked = !( Application.Current as App ).Clicked;
}
}
}
As a side note - I tried setting the value of the data trigger to just "True", and that also did not work ( the trigger did not catch, and text did not change based on setting the property to a new value ).
So why is the data-trigger not catching or working here? ( Either with the static resource or the literal value )? Even more relevant - why am I getting this error? The "After a 'DataTrigger' is in use (sealed), it cannot be modified" error? And what is the proper method of accomplishing what I am trying to do here? ( Preferably still using a data-trigger and not a converter, since I do need to switch between two values ).
The local value assigned to the TextBlock's Text property has higher precedence than the value provided by the Setter in the DataTrigger. See Dependency Property Value Precedence for details.
Set the initial Text value by another Setter:
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="Unclicked"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Clicked,
Source={x:Static Application.Current}}"
Value="{StaticResource True}">
<Setter Property="Text" Value="Clicked" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
The error message you see when you use the Boolean resource is just the XAML designer complaining. There is no error at runtime.
Related
I am not successful at getting my DataTrigger to work for binding to enum.
Each line in the ListBox is 'S', just as in the default Setter
XAML:
<Window x:Class="BindToEnumTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindToEnumTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox Grid.Column="0" x:Name="LBMain" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Column="0" x:Name="TxtType">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="S" />
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="TypeEnum.User">
<Setter Property="Text" Value="U"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Code:
namespace BindToEnumTest
{
public enum TypeEnum { None, System, User }
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public Collection<TypeEnum> TypeList = new() { TypeEnum.System, TypeEnum.User, TypeEnum.System, TypeEnum.User };
public MainWindow()
{
InitializeComponent();
LBMain.DataContext = TypeList;
}
}
}
I have tried using 'TypeEnum.User' and 'User' in the DataTrigger - no help.
Using Text="{Binding}" in the TextBlock shows 'User' and 'System' in the ListBox, so it seems to be getting the data.
How can I change this trigger to function?
A TypeEnum instance has no Type property, hence the Binding must not specify a property path:
<DataTrigger Binding="{Binding}" Value="{x:Static local:TypeEnum.User}">
Or with built-in type conversion from string to enum:
<DataTrigger Binding="{Binding}" Value="User">
I realise that WPF is still a bucket of magic to me. The problem seems to be simple. I have a user control with a button. I would like to change the button content (text) on click.
If I open the form with user control without initialising the button value and then say in
void Button_Click(object sender, RoutedEventArgs e)
{
Button.Content = "New Value";
}
it works.
If I initialise the button value dynamically in the control constructor by Button.Content = "Init Value", the second bit Button.Content = "New Value"
never happens (it happens, but button text does not show the change ever again, at least that what it seems).
So I decided to use a binding. Declared ButtonText property in MyUserControl (+ the corresponding DependencyProperty with getter and setter) and tried to do ButtonText = "Init Value"; in constructor and ButtonText = "New Value"; in Button_Click(). The first one works, the second one still does not. I assume because of the wrong data context in Button_Click()?
In MyUserControl I tried a few things including
<Button x:Name="Button"
Content="{Binding Path=ButtonText, RelativeSource={RelativeSource AncestorType=UserControl}}"
Click="Button_Click" />
<Button x:Name="Button"
Content="{Binding Path=ButtonText, Element=MyUserControl}"
Click="Button_Click" />
and nothing seems to work.
What is the easiest way to achieve what I need, i.e. both dynamic initialisation and dynamic change? With an explanation, if possible, please, why my first (direct) approach does not work and what the binding approach is missing.
Thanks a lot!
EDIT:
An alternative would be to use triggers.
This example works:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Init Value"/>
<Style.Triggers>
<Trigger Property="IsClicked" Value="True">
<Setter Property="Content" Value="New Value" />
</Trigger>
</Style.Triggers>
</Style>
My problem is that I need a DataTrigger, like this
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Init Value"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MyUserControlProperty, ElementName=MyUserControl}" Value="True">
<Setter Property="Content" Value="New Value" />
</DataTrigger>
</Style.Triggers>
</Style>
How do I make MyUserControlProperty value change propagate properly?
The funniest thing is if I open the control as a new form the initialisation of Button.Content = "Init Value" does not screw things up and everything just works. What the? Why is this simple task so hard and why so many behaviours?
Don't set Button.Content directly, when you have a binding set on it. If you set Button.Content in the Constructor you effectively remove the binding.
This works:
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Content="{Binding Path=ButtonText1, RelativeSource={RelativeSource AncestorType=Window}}" Click="Button1_OnClick"></Button>
</StackPanel>
</Window>
using System.Windows;
namespace StackOverflow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ButtonText1 = "Paul";
}
private void Button1_OnClick(object sender, RoutedEventArgs e)
{
ButtonText1 = "Maria";
}
public string ButtonText1
{
get => (string)GetValue(ButtonText1Property);
set => SetValue(ButtonText1Property, value);
}
// Using a DependencyProperty as the backing store for ButtonText1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonText1Property =
DependencyProperty.Register("ButtonText1", typeof(string), typeof(MainWindow), new PropertyMetadata("Peter"));
}
}
I searched a lot on that topic but couldnt really find a solution for this using no code behind. I know some would say using code-behind for this view related things is totally ok, but nevertheless i would like to avoid it.
I have a usercontrol which shows a "dialog" with a single textbox and an OK button. That dialog is a simple usercontrol that is placed on top of all others. By default the usercontrols visibility is set to collapsed. I would like to set the keyboardfocus to the textbox on the dialog usercontrol if the usercontrol gets visible. Is there any way to do this completely in xaml? Since my dialog-control is not visible at the time when the control is loaded, simply setting
FocusManager.FocusedElement="{Binding ElementName=tbID}"
will not work. I tried to use some kind of visibility trigger:
<TextBox Grid.Column="3"
Grid.Row="5"
Name="tbID"
VerticalAlignment="Center">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbID}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
but this doesnt work either. The trigger gets fired but the textbox doesnt get the focus. I would really appreciate any suggestions on that. Thanks in advance!
You could try using an attached behavior to set the focus. Here's some sample code:
public static class Focus
{
public static readonly DependencyProperty ShouldFocusWhenVisibleProperty =
DependencyProperty.RegisterAttached("ShouldFocusWhenVisible", typeof (bool), typeof (Focus), new PropertyMetadata(default(bool), ShouldFocusWhenVisibleChanged));
private static void ShouldFocusWhenVisibleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var uiElement = sender as UIElement;
if (uiElement == null) return;
var shouldFocus = GetShouldFocusWhenVisible(uiElement);
if (shouldFocus)
{
UpdateFocus(uiElement);
uiElement.IsVisibleChanged += UiElementOnIsVisibleChanged;
}
else
uiElement.IsVisibleChanged -= UiElementOnIsVisibleChanged;
}
private static void UiElementOnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var uiElement = sender as UIElement;
if (uiElement == null) return;
UpdateFocus(uiElement);
}
private static void UpdateFocus(UIElement uiElement)
{
if (!uiElement.IsVisible) return;
Keyboard.PrimaryDevice.Focus(uiElement);
}
public static void SetShouldFocusWhenVisible(UIElement uiElement, bool value)
{
uiElement.SetValue(ShouldFocusWhenVisibleProperty, value);
}
public static bool GetShouldFocusWhenVisible(UIElement uiElement)
{
return (bool)uiElement.GetValue(ShouldFocusWhenVisibleProperty);
}
}
Then, you apply the following code to the TextBox in your dialog: <TextBox local:Focus.ShouldFocusWhenVisible="True" />. Note that local: will need to be a reference to the namespace of the Focus class above.
I think you want to bind to the UserControl Visibility property not the TextBox
Example
<UserControl x:Class="WpfApplication7.IconButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="200" Name="_this">
<Grid>
<TextBox Name="tbID" Margin="0,12,0,0" VerticalAlignment="Top">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=_this, Path=Visibility}" Value="Visible">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbID}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</UserControl>
MSDN says "Gets or sets an arbitrary object value that can be used to store custom information about this element." which means I can store anything I want in this property.
But if you bind to this property (with property of type String having a value say "XYZ") and use it in Trigger conditions it doesn't work!
<Trigger Property="Tag" Value="XYZ">
<Setter Property="Background" Value="Red" />
</Trigger>
It does not set the background red. You can try and assume myElement to be a TextBlock! Why is it like this?
Tag has no special functionality in WPF.
This works for me:
<TextBlock Tag="{Binding Data}"
x:Name="tb">
<TextBlock.Style>
<Style>
<Style.Triggers>
<Trigger Property="TextBlock.Tag"
Value="XYZ">
<Setter Property="TextBlock.Background"
Value="Lime" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
And setting the Data object property to "XYZ" in an event.
The Tag is a construct held over from Winforms days (and possibly there from before that!). It was used as a convenient place to associate an object with a UI element, such as a FileInfo with a Button, so in the Button's event handler you could simply take the event sender, cast it to a Button, then cast the Tag value to a FileInfo and you have everything you need about the file you want to open.
There is one situation, however, where I've found the Tag is useful in WPF. I've used it as a holding spot that can be accessed by a ContextMenu MenuItem, which can't use the normal RelativeSource bindings you'd use to traverse the visual tree.
<ListBox.ItemContainerStyle>
<Style
TargetType="ListBoxItem">
<Setter
Property="Tag"
Value="{Binding ElementName=TheUserControlRootElement}" />
<Setter
Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem
Header="_Remove"
ToolTip="Remove this from this list"
CommandParameter="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
Command="{Binding PlacementTarget.Tag.Remove, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
From the ContextMenu, I cannot access the Remove command which is defined in the UserControl class where this snippet is defined. But I can bind the root to the Tag of the ListBoxItem, which I can access via the ContextMenu.PlacementTarget property. The same trick can be used when binding within a ToolTip, as the same limitations apply.
MainWindow.xaml:
<Window x:Class="wpftest.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>
<TextBlock x:Name="test" MouseDown="test_MouseDown"
Tag="{Binding TestProperty}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Tag" Value="XYZ">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new TestViewModel();
}
private void test_MouseDown(object sender, MouseButtonEventArgs e)
{
((TestViewModel)DataContext).TestProperty = "XYZ";
}
private sealed class TestViewModel : INotifyPropertyChanged
{
private string _testPropertyValue;
public string TestProperty
{
get { return _testPropertyValue; }
set
{
_testPropertyValue = value;
var handler = PropertyChanged;
if(handler != null)
handler(this, new PropertyChangedEventArgs("TestProperty"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Updated: Tag property now is bound to TestProperty.
I want to change the background color of our apps main window when a property changes. We have a business date that can be changed and I want to change the window background when it has changed from expected. I've set up a property to tell this. But can I set a style datatrigger on a window that changes itself? Or would I need to do this in the app.xaml?
I ended up kind of doing what Drew suggested. Except I didn't use a Dependency Property.
<Window.Resources>
<SolidColorBrush x:Key="windowBGBrush" Color="Green"/>
<SolidColorBrush x:Key="windowBGBrushBusinessDateChanged" Color="Red"/>
</Window.Resources>
<Window.Style >
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource windowBGBrush}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsBusinessDateChanged}" Value="true">
<Setter Property="Background" Value="{DynamicResource windowBGBrushBusinessDateChanged}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
IsBusinessDateChanged is a property on my Viewmodel that gets set by a service. I'm not sure why this was so hard.
If you're exposing a custom property on the Window just make sure it's defined as a DependencyProperty and then you should be able to use a regular trigger in the style to react to the property. Like so:
<Window.Style>
<Style TargetType="{x:Type MyWindow}">
<Style.Triggers>
<Trigger Property="MyProperty" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Style>
Here's a solution with a converter approach:
XAML:
<Window x:Class="StackOverflowTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" x:Name="window1" Width="300"
xmlns:local="clr-namespace:StackOverflowTests">
<Window.Resources>
<local:DateToColorConverter x:Key="DateToColorConverter" />
</Window.Resources>
<Window.Background>
<SolidColorBrush Color="{Binding ElementName=textBoxName, Path=Text, Converter={StaticResource DateToColorConverter}}" />
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="textBoxName" Margin="5"></TextBox>
</Grid>
</Window>
C#:
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace StackOverflowTests
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class DateToColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime date;
if (DateTime.TryParse(value.ToString(), out date))
{
if (date == DateTime.Today)
return Colors.Green;
else
return Colors.Red;
}
else
{
return Colors.Gold;
}
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
}
Maybe it's better to just bind the background with the property. You need to set the datasource of the window to the object and may need a valueconverter.