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.
Related
I am referring to the code block here, on Data triggers
<Window x:Class="WpfTutorialSamples.Styles.StyleDataTriggerSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="StyleDataTriggerSample" Height="200" Width="200">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox Name="cbSample" Content="Hello, world?" />
<TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="No" />
<Setter Property="Foreground" Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
<Setter Property="Text" Value="Yes!" />
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</Window>
To me, from programming language design point of view, the line <Style TargetType="TextBlock"> is completely unnecessary, because it is already nested inside the <TextBlock>, so of course whatever setter property should be applied to the TextBlock type. So why need the line <Style TargetType="TextBlock">?
Can <Style TargetType> be of other type except TextBlock?
So why need the line ?
A Style may be defined as a resource, i.e. not inline, and if doesn't have a TargetType specified, how is the XAML parser supposed to be able to parse it and set the properties specified by the setters? It can't. Or at least it doesn't.
Just because you can define a Style inline you are still creating an instance of the exact same class that may be used as a (global) resource and then setting a TargetType is indeed required.
Can be of other type except TextBlock?
No, apart from a type that is derived from TextBlock. If you specify another type you will get an exception at runtime when the BAML (the compiled XAML) is parsed.
You could use any class TextBlock derives from (for example FrameworkElement).
If you implement your own CustomizedTextBlock for example you are able to use styles defined for TextBlock in your project.
You find an example for this here.
I am having a problem with the style of a few items that are bound to a set of radio buttons. Basically, I have the following code for my styles:
<Window.Resources>
<Style x:Key="boxStyle" TargetType="TextBox">
<Setter Property="Background" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
Then I have two radio buttons as shown here:
<RadioButton Name="optionA" IsChecked="True">Option A</RadioButton>
<RadioButton Name="optionB'>Option B</RadioButton>
And two text boxes as shown here:
<TextBox Style="{StaticResource boxStyle}" IsEnabled="{Binding ElementName=optionA, Path=IsChecked}"/>
<TextBox Style="{StaticResource boxStyle}" IsEnabled="{Binding ElementName=optionB, Path=IsChecked}"/>
The binding works correctly (when Option A it checked, one box is enabled and the other is not). However, when either of the boxes becomes disabled, it does not follow the style defined above. The background goes to white no matter what I change the style color to.
Anyone have any ideas? Thanks.
The color used when disabled is hard-coded in the template as far as i know, you cannot easily change it unless it references a system-color in which case you can override.
The default Aero theme uses a ListBoxChrome control, not sure if that can be made to change its background accordingly, it has no template so it might be hard to modify it. You could of course throw it out and use whatever you want (which you can modify).
<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
I have a resource dictionary in my WPF application which contains the style information for the various controls.
Can it be used like the way we use in CSS in HTML? For example
p
{
margin:20px;
font:Tahoma;
}
this applies to all "p" tags in HTML. We dont have to specifically mention that in the HTML for "p" tag.
Is the same approach applicable in WPF, or do we have to specifically
mention the style
<TextBlock Text="Test" Style="{DynamicResource SomeTextblockStyle}" />
in the XAML
You can certainly set a default style for each type. You can do this within your Generic.xaml, note that I am not providing a key.
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="White"/>
</Style>
This will style every instance of a Button within your application as such.
If you were go to a XAML file and define an instance of a Button, overriding the Foreground value, that local instance will take precedence over the global style.
<Button Foreground="Black"/>
You can set style like using key
<Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
<Setter Property="Margin" Value="20"/>
<Setter Property="FontFamily" Value="Tahoma"/>
</Style>
And in the Window.Xaml
<TextBlock Text="Hello" Style="{DynamicResource myStyle}"/>
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