Given a resource dictionary loading some static resources into memory - is there any way to iterate through theResources loaded into memory? My silverlight application keeps telling me it cannot find a static resource. I wonder if I have a naming convention issue or somehting - was hoping iterating through the resources in memory would help diagnose any issue...
I have the following app.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Module1.MyApp">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FSSilverlightApp;component/TransitioningFrame.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
and content template:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TransitioningFrame" TargetType="navigation:Frame">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentPresenter Cursor="{TemplateBinding Cursor}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</ResourceDictionary>
Resources defined in Application.Resources are implicitly available to all controls in the app, not sure about merged dictionaries. You can set a breakpoint in a control constructor and inspect this.Resources, this.Resources.MergedDictionaries, Application.Resources and Application.Resources.MergedDictionaries to see if your resource is there.
Also try to put the merged dictionaries in your control, see if that makes any difference:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FSSilverlightApp;component/TransitioningFrame.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!--Use the style later in the same file-->
<navigation:Frame Style={StaticResource TransitioningFrame} />
Related
Consider I have 4 Window on my project and I try to provide specific Closing button and one Title
How could I make a object of window and all of window use it as Pattern.
Here is Example of what we have for pattern window:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" AllowsTransparency="True" >
<Grid>
<Button Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" X:Name="WindowTitle/>
</Grid>
</Window>
How could I use for all of my Window as pattern. Thanks
Actually, there is no need to write a parent window. You can use Style and Template instead. It's more convenient and is recommended by Microsoft WPF Team.
Write the code below into your App.xaml and you'll get the picture above:
<Application x:Class="Walterlv.Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
.........>
<Application.Resources>
<Style x:Key="Style.Window.Default" TargetType="Window">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
Text="{TemplateBinding Title}"/>
<Border Grid.Row="1" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<!-- This is the container to host your Window.Content -->
<ContentPresenter/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
And you can use only one property Style to share such a "pattern":
<Window x:Class="Walterlv.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{StaticResource Style.Window.Default}">
</Window>
You can define different kinds of styles in the App.xaml file and select anyone in your XxxWindow.xaml you need.
I have followed the solution of this question: how to have many wpf custom controls in the same library?
The problem now is that I can see the controls of my custom control when I add the custom control in my main view.
However, if I have the style in the generic.xaml file instead of the MyControl.xaml file, and only I have ona user control, it works.
The code of my MyControl.xaml is this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls.Calendario2">
<Style TargetType="{x:Type local:CalendarioMes2}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CalendarioMes2}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<TextBox Text="Prueba" BorderBrush="Black"/>
<Label Content="Prueba" BorderBrush="black"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
And the generic.xaml has this code:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls.Calendario2">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CustomControls.Calendario2;component/ResourceDictionaries/CalendarioMes2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Why can't I see the textBox and the label?
Thanks.
EDIT: I have use the absolute path to the file:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="F:\Alvaro\ejemplos\Calendario\CustomControls\Themes\CalendarioMes2.xaml"/>
</ResourceDictionary.MergedDictionaries>
But know I have an error in the main window that say that I can create a type from the text. The error is in the CalendarioMes2.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls.Calendario2">
<Style TargetType="{x:Type local:CalendarioMes2}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CalendarioMes2}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<TextBox Text="Prueba" BorderBrush="Black"/>
<Label Content="Prueba" BorderBrush="black"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
In the line 7:
<Style TargetType="{x:Type local:CalendarioMes2}">
But compare my code with the code of the solution in the other thread, I see that I have the same line.
This is CalendarioMes2.cs
namespace CustomControls.Calendario2
{
public class CalendarioMes2 : Control
{
static CalendarioMes2()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CalendarioMes2), new FrameworkPropertyMetadata(typeof(CalendarioMes2)));
}
}
}
EDIT: the source code is here: https://1drv.ms/u/s!AiTepLSbavZ0gRDvBPJgrxEIWS1U
I am trying to set the window icon in a WPF application that uses MahApps.Metro. I don't know how to set a static resource that I defined after the window's XAML to be the window's icon.
Below is the XAML, the resource I want is in Icons.xaml:
<controls:MetroWindow x:Class="Test1.MainWindow"
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"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Here is an example of how I use the resource on the window:
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_alien}" />
</Rectangle.Fill>
</Rectangle>
How do I set appbar_alien as the window's icon?
I've just answered to a question about icon too big in MahApps MetroWindow (Application icon stretches to title bar height when using MahApps.Metro) and same technique could give you a solution.
Basically, you have to set MetroWindow.IconTemplate property:
<MahApps:MetroWindow.IconTemplate>
<DataTemplate>
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="4"
Background="Transparent"
RenderOptions.EdgeMode="Aliased"
RenderOptions.BitmapScalingMode="HighQuality">
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_alien}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</MahApps:MetroWindow.IconTemplate>
Depending of your "/Resources/Icons.xaml", you may have to stretch/scale it.
I am attempting to apply a "text alignment" to a ContentControl. Since the ContentControl does not have a horizontal or vertical text alignment property like the TextBlock, I am attempting to use the ContentControl's HorizontalContentAlignment property.
My problem is that I can't get it to work with a ContentControl itself.
In my example, I have a content control displaying "hello world" and a button displaying "change it".
When I click the button, I set the HorizontalContentAlignment on the content control and on the button. The button's content changes, but the content control's content does not.
Here is my XAML code:
<Window x:Class="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.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ContentControl x:Name="ctrl" Width="525">
Hello World!
</ContentControl>
<Button x:Name="btn" Grid.Row="1" Content="Change It" Click="btn_Click"/>
</Grid>
</Window>
And here is my VB.NET code for the button click event:
Class MainWindow
Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then
ctrl.HorizontalContentAlignment = HorizontalAlignment.Right
btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right
Else
ctrl.HorizontalContentAlignment = HorizontalAlignment.Left
btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left
End If
ctrl.UpdateLayout()
End Sub
End Class
I am unable to replace my content controls with text blocks for various reasons, but I still need to be able to align the content.
EDIT:
While Narohi work around suggestion works, I am still confused about why the content control's HorizontalContentAlignment property doesn't align the content.
I tried a Label control (which inherits from ContentControl) and it's HorizontalContentAlignment property properly aligns the content.
(Edit again: I am no longer confused about this, it seems that the HorizontalContentAlignment isn't utilized properly in all cases.)
Here is my updated XAML code:
<Window x:Class="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>
<ControlTemplate x:Key="AlignmentAwareControl" TargetType="ContentControl">
<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ContentControl x:Name="ctrlTxt" Grid.Row="0"
Template="{StaticResource AlignmentAwareControl}"
HorizontalContentAlignment="Center" Padding="0">Hello World Content Control!</ContentControl>
<Label x:Name="ctrl" Grid.Row="1" HorizontalContentAlignment="Center" Padding="0">Hello World Label!</Label>
<ContentControl x:Name="ctrlImg" Grid.Row="2"
Template="{StaticResource AlignmentAwareControl}"
HorizontalContentAlignment="Center">
<Image Source="C:\Users\Frinavale\Pictures\Business_Woman.jpg"/>
</ContentControl>
<Button x:Name="btn" Grid.Row="3" Content="Change It" Click="btn_Click"/>
</Grid>
</Window>
Here is my updated VB.NET code:
Class MainWindow
Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then
ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right
Else
ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left
End If
ctrl.UpdateLayout()
End Sub
End Class
I'm looking forward to your advice,
-Frinny
Opening up the default control template for ContentControl in Blend reveals why your original approach did not work.
<ControlTemplate TargetType="{x:Type ContentControl}">
<ContentPresenter/>
</ControlTemplate>
The default template does nothing with the HorizontalContentAlignment property which it inherited from Control. Juxtapose this with Label's default template.
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
As we can see, the Label actually binds to the HorizontalContentAlignment. WPF controls are lookless, so there is never a guarantee that a property will be respected by the current ControlTemplate. I would speculate that the WPF designers didn't respect the HorizontalContentAlignment property because people usually place content within that is independent of the ContentControl's properties or perhaps they assumed if someone was going to use such a generic control they would provide their own template, such as...
<ContentControl x:Name="ctrl" Width="525">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid>
<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Grid>
</ControlTemplate>
</ContentControl.Template>
Hello World!
</ContentControl>
I want ask a question about that code its simple XAML code as show as below.
<Window x:Class="WpfApplication15.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" Background="Orange" FontStyle="Italic">
<StackPanel>
<Label>blo</Label>
<Button>hi</Button>
<Button>hi2</Button>
</StackPanel>
</Window>
So simply we can imagine, labels background orange but background color of button wont be orange will be as default, but all controls FontStyle will be italic, so question is that! why fontstyle of all control under root affected that but button's background doesn't??
I suppose that the ControlTemplate of the button sets the Background-brush explicitely in respect to windows ui-guidelines. Therefore property-inheritance is broken for the Control.BackgroundProperty.
To prove it, maybe this tool will help.
This is the definition of the default button template , Microsoft_Windows_Themes:ButtonChrome is doing the trick
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Microsoft_Windows_Themes:ButtonChrome>