FontSize Style Ignored by TextBlock in DataTemplate - silverlight

The style for TextBlock (below) has no effect on the DataTemplate's TextBlock. If I change TextBlock to TextBox in both the style and template, the style applies as I would expect. Why does TextBlock ignore the style?
Thank you,
Ben
<UserControl x:Class="SilverlightApplication1.MainPage"
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:l="clr-namespace:SilverlightApplication1" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20" />
</Style>
<DataTemplate DataType="l:MyObject">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</UserControl.Resources>
<StackPanel>
<ItemsControl>
<ItemsControl.Items>
<l:MyObject Name="Frank" />
</ItemsControl.Items>
</ItemsControl>
</StackPanel>
</UserControl>

Related

WPF - Set ContentTemplate with static resource child?

I am relatively new in MVVM. May i know is that a way i can get the element in my user control and bind it separately in a ContentTemplate? I draft a sample code like below.
I have a user control consists of RedStack, GreenStack, BlueStack. My goal is if condition A then show the RedStack and BlueStack in the ContentTemplate, else show all color stacks in the ContentTemplate
Color user control :
<UserControl x:Class="MapDisplay.ColorView"
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="300" d:DesignWidth="300">
<StackPanel>
<StackPanel x:Name="RedStack">
<Rectangle Fill="Red" Width="100" Height="100"/>
</StackPanel>
<StackPanel x:Name="GreenStack" >
<Rectangle Fill="Green" Width="100" Height="100"/>
</StackPanel>
<StackPanel x:Name="BlueStack" >
<Rectangle Fill="Blue" Width="100" Height="100"/>
</StackPanel>
</StackPanel>
Main user control:
<UserControl x:Class="WpfApplication.UserControl"
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"
xmlns:custom="clr-namespace:MapDisplay;assembly=MapDisplay"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d" >
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<DataTemplate DataType="{x:Type custom:ColorController}">
<custom:ColorView/>
</DataTemplate>
<DataTemplate x:Key="ColorDisplay">
<Controls:MetroAnimatedSingleRowTabControl ItemsSource="{Binding Colors}" />
</DataTemplate>
<ControlTemplate x:Key="SplitColorDisplay">
<StackPanel>
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource ColorDisplay.Colors.RedStack?}" />
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource ColorDisplay.Colors.BlueStack?}" />
</StackPanel>
</ControlTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Border>
<ContentControl DockPanel.Dock="Top" Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource ColorDisplay}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSplitColor}" Value="True">
<Setter Property="Template" Value="{StaticResource SplitColorDisplay}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Border>
</UserControl>

Unnamed Style that can be used with BasedOn in UserControls

I am looking for a XAML-only way to define an unnamed style in a particular Window that affects all TextBoxes, even those inside UserControls.
Here is an example: suppose I have one particular Window in which I want to set the Foreground property to Red for all TextBoxes, including those contained in UserControls. I am trying to do it like this:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Width="300"
SizeToContent="Height">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Setters>
<Setter Property="Foreground" Value="Red"/>
</Style.Setters>
</Style>
</Window.Resources>
<StackPanel Orientation="Vertical">
<TextBox Text="A-Regular"/>
<TextBox Text="B-Bold">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Setters>
<Setter Property="FontWeight" Value="Bold"/>
</Style.Setters>
</Style>
</TextBox.Style>
</TextBox>
<local:MyUserControl/>
<TextBox Text="C-Regular"/>
</StackPanel>
</Window>
The used UserControl looks like this:
<UserControl x:Class="WpfApplication1.MyUserControl"
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="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
<TextBox Text="Like A, but in UserControl"/>
<TextBox Text="Like B, but in UserControl">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Setters>
<Setter Property="FontWeight" Value="Bold"/>
</Style.Setters>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</UserControl>
The resulting window is:
screenshot of window
Everything is OK, except for the TextBox "Like B, but in UserControl", which is bold and black. I would have expected to see red text, like in all other TextBoxes.
Is there a way to have the unnamed style in the Window's resource affect all TextBoxes in the UserControl, even those that expand the current TextBox's style using BasedOn?
I am looking for a way where I do not have adapt all places where I "call" the UserControl (i.e. where I have <local:MyUserControl/> in the Window). Nor do I want to have to adapt the UserControl itself, because it might be within a third-party library.
Just put Style into <Application.Resources>

Window Visibility Not applying

The following code does not make the window I run invisible. What am I missing? Initially, my goal was to bind the visibility of the window to the Model View's WindowVisibility Boolean property but when that wouldn't work, I removed the conditional triggering and just assigned a style that set it to collapsed but it still doesn't work.
<Window x:Class="WindowWalker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="1000" MinHeight="10" Height="85" ResizeMode="NoResize" Topmost="True" WindowStyle="None" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" SizeToContent="Height">
<Window.Style>
<Style TargetType="{x:Type Window}">
<Setter Property="Visibility" Value="Collapsed"/>
<!--<Style.Triggers>
<DataTrigger Binding="{Binding WindowVisibility}" Value="True">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>-->
</Style>
</Window.Style>
<Window.Resources>
<ResourceDictionary>
<VisualBrush x:Key="SettingsBrush" Visual="{StaticResource appbar_settings}"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" FontSize="40" Margin="10" KeyUp="SearchBoxKeyUp"/>
<ListBox x:Name="results" ItemsSource="{Binding Results}" SelectedItem="{Binding SelectedWindowResult}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ResultWindow.Title}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>

How to disable (globally) the FocusVisualStyleKey

I want to disable (globally) the FocusVisualStyleKey.
I'm looking for a possibility, that I have not put on any element the following code:
<Style TargetType="ToggleButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
So I read this article, and found this solution:
App.xaml
<Application x:Class="WpfApplication7.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="{x:Static SystemParameters.FocusVisualStyleKey}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle StrokeThickness="0" SnapsToDevicePixels="true" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="WpfApplication7.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">
<Window.Resources>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="98,230,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="217,230,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="368,230,0,0" VerticalAlignment="Top" Width="75"/>
<ComboBox HorizontalAlignment="Left" Margin="40,40,0,0" VerticalAlignment="Top" Width="150" Height="40"/>
<ComboBox HorizontalAlignment="Left" Margin="317,40,0,0" VerticalAlignment="Top" Width="150" Height="40"/>
</Grid>
</Window>
But it doesn't work...
Does anyone have another idea?
Yes, there is a way to do this but you are not going to like it. First make another xaml dictionary. In it define a style for each control in Aero or w/e theme you're using, then set BasedOn property to implicit key.
example
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
then create a setter for FocusVisualStyle
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
Note: you'll have to do this for EVERY control.
Next merge your new themes dictionary into Window.Resources
<Window.Resources>
<ResourceDictionary Source="[your dictionary]"/>
</Window.Resources>

How do I let my UserControls use the same styles as App.xaml?

I have a UserControl with a Border element within it that I want to style with a particular Border style. It compiles but won't start, giving a XamlParseException, saying, "Cannot find resource ..."
Is there a way to do this?
Thanks.
App.xaml:
<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
<Style x:Key="FancyBorder"
TargetType="{x:Type Border}">
<Setter Property="Margin" Value="0,0,0,8"/>
<Setter Property="Padding" Value="8"/>
...
</Style>
</Application.Resources>
MainView.xaml:
<Window x:Class="WahnamProgressTracker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:uc="clr-namespace:WahnamProgressTracker.UserControls"
MinHeight="500" MinWidth="800">
<DockPanel>
<uc:MainViewMenu x:Name="menu"
DockPanel.Dock="Top" />
<StatusBar x:Name="quoteBar"
DockPanel.Dock="Bottom">
<TextBlock Text="{Binding Path=Quote.Text, Mode=OneWay}" />
</StatusBar>
<uc:MainViewNavigation x:Name="navigationBar"
DockPanel.Dock="Left" />
<uc:ProgressGraph x:Name="graph" />
</DockPanel>
MainViewNavigation.xaml (user control):
<UserControl x:Class="WahnamProgressTracker.UserControls.MainViewNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border Style="{StaticResource FancyBorder}">
...
</Border>
</UserControl>
Can you post a sample of what you mean? The only case in which your issue can occur is if the User Control is created and then rendered outside your application's visual tree.
The XAML below works for me:
App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Application.Resources>
</Application>
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<local:UserControl1 />
</Grid>
</Window>
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TextBlock Style="{StaticResource myStyle}">HEY!</TextBlock>
</Grid>
</UserControl>

Resources