Textbox Disabled background from a local trigger without using template - wpf

<Window x:Class="AiweeeTest.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>
<Grid.Resources>
<SolidColorBrush x:Key="backcolorType" Color="Red"></SolidColorBrush>
<SolidColorBrush x:Key="forecolorType" Color="Green"></SolidColorBrush>
<Style x:Key="TextboxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="{StaticResource backcolorType}"/>
<Setter Property="Foreground" Value="{StaticResource forecolorType}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<TextBox Name="textbox1" Width="100" Height="25" Style="{StaticResource TextboxStyle}" IsEnabled="False"/>
</Grid>
</Window>
I am not able to update the background of a textbox using the trigger defined above, however when I copy the entire textbox control template template and replace the "DisabledBackgroundBrush" with my own color it does. What's the difference, I've already seen some links over this matter; however I am not able to understand the reason behind it. As I understand, Triggers are fired in the order they are defined, then the trigger defined locally in the window should be able to override the background color of the textbox when disabled. Please clarify.
PS: I am not trying to achieve anything special here, but just want to understand why is this so. This gives me a bit of frustration of WPF not being intuitive for situations like such.

I am guessing it has something to do with the order in which WPF will apply values for a DependencyProperty. This MSDN article has some good information on Dependency Property Precedence
Basically the order goes:
Property system coercion
Active animations, or animations with a Hold behavior.
Local value
TemplatedParent template properties
Triggers from the TemplatedParent template
Property sets (typically through XAML attributes) in the TemplatedParent template
Implicit style
Style triggers
Template triggers
Style setters
Default (theme) style
Active triggers in the theme style
Setters in the theme style
Inheritance
Default value from dependency property metadata

Related

css !Important in WPF style

I have text block with MaxWidth="80" and want change on Style without remove MaxWidth="80"
the TextBlock is at third party control
for sample:
<Window.Resources>
<Style TargetType="TextBox" >
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="10"/>
</Style>
</Window.Resources>
<Grid>
<TextBox FontSize="45" Foreground="Blue" Text="OH My God"/>
</Grid>
Not sure i'm answering what you asked, since, as other users pointed out, the question is not well written. But...
A setter within a style has less priority than setting the property directly on the instance of the object.
So, even though your style declares
<Setter Property="MaxWidth" Value="80"/>
If you write your textblock/textbox like this
<TextBlock MaxWidth="100"/>
the 100 will prevail on the 80, thus the max width will be 100.
In the WPF there is a strict rule which property is about to be used.
So you can:
Define a default style with the property = 100.
Define another style based on the default and override the property as you wish.
Apply the new created style to your TextBox.
And attribute has more power than a style.

Binding Window Title through Style && triggers in XAML

I've code like that
<Window x:Class="SolutionName.ClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" WindowStartupLocation="CenterOwner">
<Window.Style>
<Style TargetType="Window">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ItemKind}" Value="SomeKind">
<Setter Property="Title" Value="SomeTitle"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ItemKind}" Value="SomeKind2">
<Setter Property="Title" Value="SomeTitle"/>
</DataTrigger>
I want is to change window title depending on a property ItemKind implemented in the viewmodel (set as datacontext). The code above won't work and I'm really confused because can't find any mistakes.
The code seems fine and working at my end. Have you implemented INotifyPropertyChanged in your ViewModel class. You need to implement it to so as to propagate any change in property value in your ViewModel class to reflect back on your UI.
How to: Implement Property Change Notification

WPF: Style ListBoxItem to unify colors

I am starting with WPF development on a touch device. Although .NET and WPF seem to be an amazing technology, I am somewhat lost.
I am developing a generic control. I defined an UserControl which contains a Grid, some buttons and a SurfaceListBox. In the actual C#-code I am handling events and add new elements to the list box by manipulation the listItems-Attribute. This works fine so far.
Now I would like to change the style of the list items - I would like to make their background transparent if they are not selected, and completely white if they are. Unfortunately the following piece of code simply does not work. It just sets the background color of the list item text to white, but not the whole item.
Update: Now it works!
<UserControl x:Class="SGEditor.MyControl"
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"
xmlns:my="http://schemas.microsoft.com/surface/2008" UseLayoutRounding="True">
<UserControl.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="Background" Value="Transparent" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<my:SurfaceListBox Width="300" Height="300" />
</Grid>
</UserControl>
Thank you!
Hans
I don't know exactly how to do what you want to do, but I'm pretty sure you can't reassign a static system value like you're trying to do in x:Key. That's just a name you use to reference the resource later in the xaml, like MyBackgroundColor or whatever.
I think what you need to do is something with triggers in the style for your control that set the appropriate properties for the elements you want to change. Hopefully someone else will shed more light on that, since I rarely do that fancy stuff. =)

Implicit xaml style overwritten by explicit style

I Have this very simple xaml style problem in Silverlight4.
When I set a style on element through explicit styling my implicit style is removed?
I created a simple example to illustrate the problem.
<UserControl.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="red"/>
</Style>
<Style TargetType="FrameworkElement" x:Key="test">
<Setter Property="Margin" Value="20"/>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Style="{StaticResource test}" Height="40" Width="120"> </TextBox>
</Grid>
</UserControl>
So when I remove the Style="{StaticResource test}
the implicit style is applied again.
Note: this is oversimplified example. So moving the margin to the texbox style is not an option because the real project contains larger xaml styling.
Does somebody know a solution or can confirm that this isn't possible?
Thx in advance!
Take a look at the BasedOn style property. This will allow you to inherit from a base style, to use the base properties and add some changes.
Now, there is a problem with BasedOn - it can't be used with an implicit style. However, this is pretty easy to work around. This article demonstrates the work around and will also explain the BasedOn property a bit better:
Silverlight how-to: Inherit from an Implicit Style

How to change WPF Listbox/ListBoxItem DataTemplate for selected item WITHOUT affecting style & theming?

This question is very similar to Change WPF DataTemplate..., which I have read and implemented. It worked beautifully at first, but I ran into a problem with it.
That problem is that, when using themes in your application such as those in the WPF Futures project (e.g. Expression Dark), the ListBoxItems all revert back to the default WPF styling. This breaks the theme for those elements and, for example, produces black text on black background where the text would otherwise be white. This also affected my TreeView, and presumably would affect other similar controls.
I think this is because conflicting styles are being set for ListBox.ItemContainerStyle--one from the theme and one for switching data templates.
I've looked around for other solutions, but haven't found anything yet. Here are the leads or ideas I've had so far:
Subclassing DataTemplateSelector and setting it to ListBox.ItemTemplateSelector. (The current best bet).
Somehow, somewhere use a Trigger, DataTrigger, or EventTrigger.
Give up on themes.
Somehow hack the functionality I want into the theme.
Somehow make my custom ItemContainerStyle somehow inherit it's colors and eye candy from the theme's style. (I tried it briefly, and it didn't work.)
Here is my ListBox and related pieces:
<Window.Resources>
<DataTemplate x:Key="NormalTemplate">
...
</DataTemplate>
<DataTemplate x:Key="SelectedTemplate">
...
</DataTemplate>
</Window.Resources>
<ListBox x:Name="RegisterListBox" Grid.Row="0"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Adjustments}">
<!-- this is from the post referenced above -->
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContentTemplate" Value="{StaticResource NormalTemplate}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
The Listbox.DataContext is set in code to enable the ItemsSource binding.
Any ideas how I can achieve the kind of functionality described above while maintaining seamless support for themes?
Have you tried doing something like this?
<ListBox.ItemContainerStyle>
<Style
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"> <=====
...
The idea is that the framework will first go look for a style with a key equal to typeof(ListBoxItem), it will find it in the themes, and then your style will just extend the themed one with your specific details.

Resources