How to create my style based on SubheaderTextBlockStyle - c#

I am trying to create a new style based on an existing style 'SubheaderTextBlockStyle'.
I did:
<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource SubheaderTextBlockStyle}">
But It can't compile, I get message saying 'Style BasedOn property must be a Style, not 'ThemeResourceExtension' object.
How to get around that problem?

SubheaderTextBlockStyle is a WP8.1 runtime thing.
This works for me using the WP8.1 RT template. Where are you defining your <Style>?
<Page.Resources>
<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource SubheaderTextBlockStyle}">
<Setter Property="FontSize" Value="50"/>
</Style>
</Page.Resources>
<Grid>
<TextBlock Text="TEST" Style="{StaticResource HeaderTextBlockStyle}"></TextBlock>
</Grid>

Related

UWP AutoSuggestBox QueryIcon disappears after Style added

When I added style to AutoSuggestBox Query Icon disappears. Any solutions?
<Page.Resources>
<Style x:Key="AutoSuggestBoxStyle" TargetType="AutoSuggestBox">
<Setter Property="TextBoxStyle">
<Setter.Value>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
</Style>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
Page Resources Style
<AutoSuggestBox
x:Name="autoSuggestBox"
Height="40"
Margin="24,44,24,0"
Text=""
PlaceholderText="Wyszukaj serial..."
QuerySubmitted="autoSuggestBox_QuerySubmitted"
SuggestionChosen="autoSuggestBox_SuggestionChosen"
TextChanged="autoSuggestBox_TextChanged"
QueryIcon="Find"
Style="{StaticResource AutoSuggestBoxStyle}"/>
XML AutoSuggestBox
Here is a better way.
You can define another TextBox style which is based on the existing style AutoSuggestBoxTextBoxStyle.
So you simply put the following into your resource dictionary.
<Style x:Key="BigAutoSuggestBoxTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource AutoSuggestBoxTextBoxStyle}">
<Setter Property="FontSize" Value="20" />
</Style>
Then, just reference it on your AutoSuggestBox.
<AutoSuggestBox QueryIcon="Find"
TextBoxStyle="{StaticResource BigAutoSuggestBoxTextBoxStyle}" />
Ok, I have a solution:
1) First I edited template copy, after PPM in design mode on AutoSuggestBox
2) Then I set FontSize in Resources.

How to style a WPF AutoCompleteBox?

Probably a really simple mistake, I have downloaded the WPF toolkit and placed an AutoCompleteBox on my window. How can I create a style for this control? I have tried to follow msdn but no luck so far...
http://msdn.microsoft.com/en-us/library/dd728668(v=vs.95).aspx
On my window I have:
<Controls:AutoCompleteBox style="{StaticResource acInput}"/>
In my styles I have the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:input="clr-namespace:System.Windows.Controls.Input"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Style x:Key="acInput" TargetType="input:AutoCompleteBox">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
</ResourceDictionary>
But I have the error:
The name "AutoCompleteBox" does not exist in the namespace "clrnamespace:System.Windows.Controls.Input".
Add the following namespace reference:
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Solved it:
Simply changed TargetType to "Controls:AutoCompleteBox"
<Style x:Key="acInput" TargetType="Controls:AutoCompleteBox">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>

How to have multiple window resource styles for same element type in WPF

I have an element in my window, as below:
<Grid>
<DockPanel LastChildFill="True">
<Label Name="StatisticsLabel" DockPanel.Dock="Bottom"></Label>
<RichTextBox Style="{StaticResource FocusMode}" Name="RichTextBox1" />
</DockPanel>
</Grid>
I would like to swith between two styles at runtime depending of the state I need the control to be in.
I had assumed I could use the following code:
<Window.Resources>
<Style x:Name="FocusMode" TargetType="RichTextBox">
<Setter Property="VerticalScrollBarVisibility" Value="Disabled"></Setter>
</Style>
<Style x:Name="NormalMode" TargetType="RichTextBox">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
</Style>
</Window.Resources>
Of course this isn't working.
Why does WPF not support multiple styles per element? Seems like a pretty basic requirement?
Otherwise, how do I achieve this?
Sorry figured it out, instead of x:Name use x:Key as below:
<Window.Resources>
<Style x:Key="FocusMode" TargetType="RichTextBox">
<Setter Property="VerticalScrollBarVisibility" Value="Disabled"></Setter>
</Style>
<Style x:Key="NormalMode" TargetType="RichTextBox">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
</Style>
I'd have a look at style triggers. You can probably get a good start on the subject from this post: How to make Style.Triggers trigger a different named style to be applied

Unset a value in a custom Style?

I my WPF application I'd like to change the text color by setting Foreground on the main Window like
<Window Foreground="Red">
<TextBlock Text="Hello World" />
</Window>
This works fine for TextBlocks, but if I add a Button, the font there stays black, since Button has a Setter for Foreground in its default style. can I make a new default style for Button based on the original one, but removing the Foregound setter?
I haven't tested this, but you could try using a base style in an appropriate resource collection (e.g. at the application level in App.xaml), and then create an implicit style for each type of control based on this base style.
<Style TargetType="{x:Type Control}" x:Key="DefaultControlStyle">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultControlStyle}" />
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource DefaultControlStyle}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultControlStyle}" />
<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource DefaultControlStyle}" />
You could also use a relative source binding trick described here.

Using usercontrols with static resources from external assembly

It sounds very simple and it's killing me!
I'm trying to use one usercontrol with styles from a ResourceDictionary of an external assembly, but I get an exception at runtime.
Here is how to reproduce:
Create a silverlight class library called MyControls.dll
Create an usercontrol called SuperControl:
<UserControl.Resources>
<ResourceDictionary Source="MyControls;component/Styles.xaml" x:Key="Styles" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Style="{StaticResource MyStyle}" Text="Hello"/>
</Grid>
Create a Styles.xaml ResourceDictionary and add:
<Style x:Key="MyStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Margin" Value="0,15,0,4"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
Create a Silverlight Application Called SL and add Mycontrols as reference
In MainPage.xaml Grid, add:
<MyControls:SuperControl />
It will compile, but running the application you get "Failed to assign to property 'System.Windows.ResourceDictionary.Source'. [Line: 10 Position: 36]"
I added this to the application's App.xaml
<ResourceDictionary Source="/MyControls;component/Styles.xaml" />
Same error... :(
Any thoughts?
Step 2.
You forgot /.
Source="MyControls;component/Styles.xaml"
Write
Source="/MyControls;component/Styles.xaml"

Categories

Resources