I am quite new to WPF.When moving the wpf project form one PC to other i find that the resources are not found:
<Window x:Class="BillingPad.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:BillingPad"
Title="MainWindow" Height="750" Width="810">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="ButtonPref">
<Setter Property="FontSize" Value="48" />
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White" />
</Style>
<Style x:Key="myStyle" TargetType="Button">
<Setter Property="Background" Value="Orange" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Padding" Value="8,4" />
<Setter Property="Margin" Value="4" />
</Style>
</Window.Resources>
......
From the code I am trying to get access to this resource using:
Button b = new Button();
b.Content = "Segundo bottón";
b.Style = (Style) (this.FindResource("ButtonPref"));
This throws the following exception:
<ExceptionString > System.Windows.ResourceReferenceKeyNotFoundException:
Resource 'ButtonPref' not found
I do not know why it does not reach the resource as it is saved in MainWindow.xaml within scope... I am missing something or should change any in the configuration I am not aware of.
Thanks
Related
I am using a TextBlock style.But it looks bad.
Am I missing code may have been written?
How can I solve this problem?
Here is code:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF353535"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="TextOptions.TextHintingMode" Value="Fixed"/>
<Setter Property="RenderOptions.ClearTypeHint" Value="Auto"/>
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType"/>
<Setter Property="TextOptions.TextFormattingMode" Value="Ideal"/>
<Setter Property="FontFamily" Value="Tahoma"/>
</Style>
TextFormattingMode.Ideal is far from Ideal :). You should use Display for small-to-medium text. More on text formatting here.
Here they are compared:
<Window
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:WpfApplication9" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF353535" />
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="TextOptions.TextHintingMode" Value="Fixed" />
<Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="FontFamily" Value="Tahoma" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>This text has TextOptions.TextRendering mode set to Display</TextBlock>
<TextBlock TextOptions.TextFormattingMode="Ideal">This text has TextOptions.TextRendering mode set to Ideal</TextBlock>
</StackPanel>
</Window>
How can I change the background of a MetroWindow?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Behaviours="clr-namespace:MahApps.Metro.Behaviours"
xmlns:Converters="clr-namespace:MahApps.Metro.Converters"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Style BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" TargetType="Controls:MetroWindow">
<Setter Property="Background" Value="LightGray" />
<Setter Property="BorderBrush" Value="#FFB9B9B9" />
<Setter Property="BorderThickness" Value="0,1,0,0" />
</Style>
create a style with a key (and put the style in your App.xaml or in a resource dictionary and put this in your App.xaml)
<Style x:Key="CustomMetroWindowStyle" TargetType="{x:Type Controls:MetroWindow}">
<Setter Property="Background"
Value="LightGray" />
<Setter Property="BorderBrush"
Value="#FFB9B9B9" />
<Setter Property="BorderThickness"
Value="0,1,0,0" />
</Style>
and use it like this
<Controls:MetroWindow x:Class="MetroDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{DynamicResource CustomMetroWindowStyle}">
</Controls:MetroWindow>
Style definition in Resources/Shared.xaml (updated):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:Double x:Key="fullHeight" >26</system:Double>
<system:Double x:Key="halfHeight" >16</system:Double>
<Thickness x:Key="m">10</Thickness>
<Style TargetType="Button">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
<Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="Label">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
<Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="PasswordBox">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
<Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="ListView">
<Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
<Setter Property="Margin" Value="{StaticResource m}"/>
<Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Margin" Value="{StaticResource m}"/>
</Style>
</ResourceDictionary>
Window:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Shared.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
User control:
<StackPanel Orientation="Horizontal">
<Label Content="Text" Background="AliceBlue"/>
<Label Content="{Binding DecimalValue, FallbackValue=50}" Background="Aquamarine"/>
</StackPanel>
Model:
private decimal _DecimalValue;
public decimal DecimalValue
{
get { return _DecimalValue; }
set
{
if (_DecimalValue != value)
{
_DecimalValue = value;
NotifyOfPropertyChange();
}
}
}
I'm using Caliburn.Micro if it makes any difference.
Result:
Why?
Update: After some Snooping, it turns out that the inner TextBlock of the first Label has margin of 0 and Value Source is Default and for the second it's 10 and Style.
Update 2: After reading up this question it turns out that defined TextBlock style should not be applied to TextBlocks inside Labels. So it seems that existence of binding on a Label somehow changes that.
You must have some other style affecting it.
My best guess would be check your Padding properties, because when I copy and paste your styles to a new project, the heights and margins are the same as your image, however the Padding is different.
Your Labels are actually getting rendered like this:
<Label>
<Border>
<ContentPresenter>
<TextBlock />
</ContentPresenter>
</Border>
</Label>
By messing around with Snoop, I can duplicate your image by altering the Padding of the Border object, so check your XAML to see if you have any implicit styles that change the Padding of your Border tags
Update
After adding the extra styles you've added to your question, I am able to reproduce the results you are getting.
The problem appears to be that the implicit style for your TextBlock is being applied to the TextBlock inside the bound label, but not to the unbound one.
It should be noted this only happens when binding to a decimal value, not to a string.
I suspect this is related to the fact that implicit styles are not meant to cross template boundaries, unless the element inherits from Control. Label inherits from Control, however TextBlock does not.
Since this only happens when binding to a numeric value, my best guess is that the process that determines how to draw a Decimal for Label.Content identifies the parent control as a Label, while the process that writes a string to Label.Content automatically knows to use a TextBlock, and does not apply the implicit styles.
Implicit Style not working in App.xaml, but is working with local page resources. How do I make a global style for a control?
<navigation:Page.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
</navigation:Page.Resources>
In App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StilDict.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In StilDict.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
,,,, and what you use else>
<Style x:Key="ButtonStyleNavyOnRed" TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
using style anywhere on your project
<UserControl>
<Button Style={StaticResource ButtonStyleNavyOnRed} Content="Yahoo! :)"/>
</UserControl>
Important Note if you delete x:Key="ButtonStyleNavyOnRed" part all of your Target types get this style, but not Button derived objects. http://msdn.microsoft.com/en-us/library/system.windows.style(v=vs.95).aspx
Hope Helps!
This works for buttons!
<Style TargetType="ButtonBase">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
Well i have my file Styles.xaml thats merged in the Application.xaml so it applies to every thing..
here are my styles
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="Button" BasedOn="{StaticResource baseStyle}">
<Setter Property="Margin" Value="2,0,2,0"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FontSize" Value="50"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
When im in the editor this seems to work but when i run the application the font-size of the buttons are shrinked to their normal sizes..
My guess is that the buttons create a TextBlock when their content is set to a string and then use the textblock style.. but how can i override this?
You're right about
My guess is that the buttons create a
TextBlock when their content is set to
a string and then use the textblock
style
. See this post.
A workaround is to define a
DataTemplate for System.String, where
we can explicitly use a default
TextBlock to display the content. You
can place that DataTemplate in the
same dictionary you define the
TextBlock style so that this
DataTemplate will be applied to
whatever ContentPresenter effected by
your style.
So adding the DataTemplate at the end to Styles.xaml will fix the problem
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}">
<Setter Property="Margin" Value="2,0,2,0"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="50"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontSize" Value="24"/>
</Style>
<DataTemplate DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}"/>
</TextBlock.Resources>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
This will keep your Style for a TextBlock but the TextBlock created in a Button for example won't be effected by it
I tried your styles, and it works well. So your styles are not the problem. I think it's the place you merged the style as you wrote. You'd better put your ResourceDictionary Styles.xaml in your MainWindow file instead of your Application.xaml.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBlock>TTT</TextBlock>
<Button>BBB</Button>
</StackPanel>
</Window>
But your problem remains unclear, if it's not the solution could you clarify a bit more the way you use your styles by posting this part of your code?