Textbox style trigger with "IsReadOnly" not working - c#

I've made an application resource with a style which should be triggered if the textbox has the "IsReadOnly" property. Looks like this:
<Application.Resources>
<Style TargetType="{x:Type TextBox}" >
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Trigger.Setters>
<Setter Property="Background" Value="Black" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
However, the program doesn't react to this. It works when I use IsEnabled=True. However IsEnabled=False doesn't work either. So, question: Do the triggers only work if you check for "True"? And is IsReadOnly not supported at all? If so: How do I know which control properties are actually supported?

See an answer for this problem by following the provided link:
DataTrigger problem with textbox

Related

C# WPF Xaml: Globally set all text in a view to one color, and all backgrounds to another

Is there a way (using data binding or simply xaml) to set the background of all elements in a view to one color, and all text to another?
I know I can edit each element in the view one by one, but I I'd like to see if this is possible with settings at a global level. Kind of like how everything by default is set to black on white.
I guess what I'm asking is if there is a feature/setting of a WPF application that offers what I'm looking for, and/or what I should search to find an answer online.
My project isn't using anything but what visual studio offers when you create a WPF project, so I can't use a prism or mvvm light approach.
Thanks in advance for your answer!
Globally...or simply XAML...
if there is a feature/setting of a WPF application that offers what I'm looking for
In Application Resource add style like this:
<Style TargetType="Control">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
Based on your target element you want to set background.
When you say "the background of all elements in a view" you should be more specific, If by 'element' you mean UIElement then there is no Background property in UIElement. If it means Control then not all UIElementsderive from Control (e.g. TextBlock) and finally if it means every UIElement derived type defined in your view that have a Background property, then you should add different styles for each type without setting the x:key to the YourView.Resources like this:
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
...
</Window.Resources>
uses controls collection through which you can control all controls in WPF

Bind Path in ResourceDictionary's dependant on VM property

I've created a ResourceDictionary that defines a bunch of System.Windows.Shapes.Path that are used in the ContentPresenter of a Button ControlTemplate.
I'd like to change one of the Paths out based on a ViewModel property. If true the button uses one path from the ResourceDictionary, if false a different one.
Currently I just reference a StaticResource in the xaml to point directly to the path I want displayed.
What's the best way to do this?
You would have to modify the content template of the button in style by referring to the elements from you resource dictionary.
Something like this:
<Button.Style>
<Style TargetType="Button">
<Setter Property="ContentTemplate" Value="{StaticResource cp2}"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource cp1}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
I have used mouse over property as my trigger to change the content template.
You can use DataTrigger instead of Trigger

Add ErrorTemplate to a MahApp control without overwriting its default style

I am using MahApps and working on implementing validation for TextBoxes. MahApps provides some nice properties in TextBoxes, as Controls:TextBoxHelper.Watermark and Controls:TextBoxHelper.ClearTextButton. I am writing an ErrorTemplate using my style but I overwrite the default template of the TextBox and lose those Metro properties. How can I achieve my goal without losing the template:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorToolTipTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
You should define your new style based on existing to keep everything it has. I guess in case of MahApps it will be MetroTextBox:
<Style TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}">
<!-- your properties go here -->
</Style>
The BasedOn is the key to success. However, it's likely, that you're using MahApps controls not in just one place. Therefore, try to make your style more generic and avoid direct references to MetroTextBox resource in the BasedOn.
The more good looking XAML style would be like that:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- your properties go here -->
</Style>

Event Trigger for Button in XAML - No Code Behind

Im trying to make an event trigger using a button to tell the ListView each time to resize it columns automatically.
ive used different methods but non of them worked, the event is in this code defined inside the ListView and im trying through DataBinding to take the action (IsPressed) from the Button trigger the ListView.
when i try to execute the code i get the following error
The tag 'Binding' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
im doing the DataBinding in a wrong way? will this code in this way work at all?
thanks in advance!
<ListView.Style>
<Style>
<Style.Triggers>
<Trigger Binding = "{Binding ElementName=Button1,Path=IsPressed}" Value="True">
<Setter Property="GridViewColumn.Width" Value="Auto"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Style>
To bind a Trigger to a property outside the element scope use DataTrigger
Example:
<DataTrigger Binding="{Binding ElementName=Button1, Path=IsPressed}" Value="True">
<Setter Property="GridViewColumn.Width" Value="Auto"/>
</DataTrigger>
A regular trigger(Property Trigger) only responds to dependency properties, which you are trying as of now:
<Style.Triggers>
<Trigger .....
Use DataTrigger, it can be bound to another control
<Style.Triggers>
<DataTrigger Binding="{Binding

DynamicResource throws an exception

I have to apply the following style to my ListViewItem:
<UserControl.Resources>
<local:Look x:Key="ListViewItemLook" Background="Magenta"/>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding Source={DynamicResource ListViewItemLook}, Path=Background}"/>
</Trigger>
</Style.Triggers>
</Style>
But i get an exception, i try to change:
<Setter Property="Background" Value="{Binding Path=Background}"/>
And add to the Style:
<Setter Property="DataContext" Value="{DynamicResource ListViewItemLook}"/>
But is does not work. I can't bind to a StaticResource because I need to set the BackGround property run-time.
What have I to do? Thanks.
If you want both local:Look and the setter to refer to the same color, perform a small refactor:
Pull the color out into a separate SolidColorBrush and make both items refer to it:
<SolidColorBrush x:Key="SelectedListViewItemBackground" Color="Magenta" />
<local:Look x:Key="whatever" Background="{StaticResource SelectedListViewItemBackground}" />
<Setter Property="Background" Value="{StaticResource SelectedListViewItemBackground}" />
If you're trying to do something else, I can't figure out what it is because the question doesn't make sense.
As far as I am aware, DynamicResource extension uses the DependencyProperty mechanism (pretty much like a binding). Therefore you cannot set Source property of a Binding object with DynamicResource because it is not a DependencyProperty.
In addition, if you want to change the Background property of Look but not the Look itself in resources; then setting Look as a static resource to the binding property should not be a problem. Of course Background property of Look class should either trigger a PropertyChanged event or be a DependencyProperty itself.

Categories

Resources