I am creating a WPF application which behaves like a 'Windows Wizard', when I press a button in MainWindow it should navigate to say Page2.
For Page2 I added a new WPF page from VisualStudio. Is there any way for this new page to inherit some attributes from main window, like background,dimension,title,etc since most of the attributes are same.
you can use ResourceDictionaries for stuff like Color Attributes etc.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="LimeColor">#FFA4C400</Color>
<SolidColorBrush Color="{StaticResource LimeColor}" x:Key="Lime" />
</ResourceDictionary>
App.xaml
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/YourDictionary.xaml" />
</Application.Resources>
For the correct Source see: Pack URIs in WPF
Page and Window
for example:
<Grid Background="{StaticResource Lime}">
Or you can define Styles and Templates in WPF for any ControlTypes.
For further information : Control Customization on MSDN
Related
Problem: The resource dictionary does not load at run time, but displays correctly in the designer. If I change the color in Dark.xaml to green, I can notice in the designer that my Window background (Layout.xaml) turns green. but when I compile the project in Debug mode, the background is just transparent !!
What I have tried in App.xaml:
<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" x:Class="StackOverflow.App">
<Application.Resources>
<ResourceDictionary Source="#"/>
</Application.Resources>
</Application>
And:
<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" x:Class="StackOverflow.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="#"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In the source value, I tried these 3 possibilities:
Theme/Dark.xaml
/StackOverflow;component/Theme/Dark.xaml
pack://application:,,,/StackOverflow;component/Theme/Dark.xaml
App.xaml.cs:
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Window Window = Server.Status() is StatusCode.OK ? new Layout() : new Login();
Window.ShowDialog();
Current.Shutdown();
Dark.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="StackOverflow.Theme.Dark" xmlns:local="clr-namespace:StackOverflow.Theme">
<SolidColorBrush x:Key="Layout.Background">Red</SolidColorBrush>
</ResourceDictionary>
Dark.xaml: File Properties
Build Action: Page
Copy to Output Directory: Do not copy
Custom Tool: XamlIntelliSenseFileGenerator
Custom Tool Namespace:
Dark.cs:
namespace StackOverflow.Theme {
public partial class Dark : ResourceDictionary {
public Dark(){
InitializeComponent();
}
}
}
Dark.cs: File Properties
Build Action: Compile
Copy to Output Directory: Do not copy
Custom Tool:
Custom Tool Namespace:
Note: These two files (Dark.xaml / Dark.cs) are nested using the File Nesting extension (Keith Wilson).
Layout.xaml:
<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:StackOverflow.XAML"
x:Class="StackOverflow.XAML.Layout"
Background="{DynamicResource Layout.Background}"
AllowsTransparency="True"/>
Project File Structure:
XAML
Layout.xaml
Theme
Dark.xaml
App.xaml
Other information:
Solution Configuration: Debug
Solution Platform: x64
Microsoft Visual Studio Community 2019 v16.7.3
Update:
For some reason the problem reappeared with the Application StartUpUri, if you noticed that I didn't use this attribute because I have to verify the user's login before giving them access to the main window. So behind the code in App.xaml.cs I call Login.ShowDialog() and Layout.ShowDialog(). After setting this attribute to Layout Window, I can notice that the application is consuming the resources defined under Dark.cs, so if I change this line:
<SolidColorBrush x:Key="Layout.Background">Lime</SolidColorBrush>
The layout background changed at runtime to the color Lime, but I'm wondering if it is possible to use the same resources defined in App.xaml in all project windows, in my case StartUpUri will be Login.xaml, and under Login if the user info is correct I can prevent this window from showing and show my layout window. but the question here is whether this resource named "Layout.Background" can be used in both windows?
I got the idea that the app resource dictionary defined in App.xaml is like a super global resource, which I can use whenever I want. Is it correct? because this result does not seem to support it.
i am Using the Fluent-Ribbons to create my view in wpf.
to have a better organisation of my code i wanted to put the different RibbonTabItems into their own files.
The problem is:
the designer does not show the content of the ribbontab.
I only see the a blank page.
Is there a way to make the RibbonTab visible in the designer?
If anyone else lands here from google, I was having this situation where the ribbon was not only invisible in the designer, but also not showing at runtime. After spending some time, I found that you have import generic fluent ribbon theme resources into your Window or Application Resources section, like this:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Fluent;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
After this my ribbon started showing in the designer as well as at runtime.
You can also use other themes in the same way. Just override the generic theme like this:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Fluent;component/Themes/Generic.xaml" />
<!-- change "Cobalt" to the color you want -->
<ResourceDictionary Source="pack://application:,,,/Fluent;component/Themes/Themes/Dark.Cobalt.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Hope this helps someone down the road.
Credit to the project documentation at github.
I would like to have a static list of Styles in Xaml
So far I have tried:
<local:Styles xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Core;assembly=MyApp.Core">
<Style x:Key="labelStyle" TargetType="Label">
<Setter Property="TextColor" Value="Green" />
</Style>
</local:Styles>
Code Behind
public partial class Styles : List<Style>
{
public Styles()
{
}
}
but when I do
var styles = new Styles();
The class is empty.
As an aside I can't use Application Resources or ResourceDictionary
You can place your styles in a ResourceDictionary (Add -> New Item -> Resource Dictionary):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Your styles here -->
<Style ...
</ResourceDictionary>
Don't forget that you will need to add a reference to it in App.xaml:
<Application x:Class="Your.App.Namespace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Your.App.Namespace;component/Path/To/Dictionary.xaml"/>
...
To get hold of these styles in code-behind, you can use the FindResource method:
Style myStyle = App.Current.FindResource("MyStyleKey") as Style;
One additional Idea that comes to mind
Create a standard Xamarin.Forms.Solution
Mark up the Application XAML with your Styles
Instantiate it and Serialize the Application.Resource to XML
Go back to your MVVMCross app and deserialize it on load and assign it to each page at construction or Application.Current.Resources. Once you have the format for the XML you'd be able to edit it directly and it would be portable from project to project basically a css style sheet(in xml format) for Xamarin Forms might be fun.
I have a Window in WPF which simply contains a Frame element. The Frame displays a Page; which Page is displayed changes based on user interaction.
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="720" Width="1280">
<Grid>
<Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
</Grid>
</Window>
I would like all Pages that appear in that Frame to share a common Resource Dictionary so that they may all be styled in a common way.
Right now I have something like this in every page that this Window loads:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>
I was hoping that I might just be able to set the resource dictionary on the Window, and they would "inherit" those resources, but that does not appear to be the case. I tried something like this, but the styles found in MenuStyle.xaml are not applied the the controls inside the Page loaded by the Frame:
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="720" Width="1280">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
</Grid>
</Window>
Is there are way to define styles at the Window level such that all pages loaded in child Frames will use those styles?
Note: I do not want to apply these styles to ALL windows in my application, so putting this ResourceDictionary in my App.xaml does not appear to be a valid solution.
If you want to write it once to avoid code duplicates, you can write it in code behind. On frame ContentRendered you can write a code to add resource to the page which is being loaded.
<Frame Name="fr_View" ContentRendered="fr_View_ContentRendered"/>
private void fr_View_ContentRendered(object sender, System.EventArgs e)
{
ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
(fr_View.Content as System.Windows.Controls.Page).Resources.MergedDictionaries.Add(myResourceDictionary);
}
Take a look at this link:
Set up application resources from code
We migrate from WinForms to WPF... slowly =)
No we use WPF User Controls with ElementHost.
Is it possible to define application wide resources in this context? In pure WPF Application.Resources stands for it. But there's no WPF App when integrating with WinForms.
You can use a WPF application object even if your project is a WinForms one with a few separate WPF forms or controls. The object won't be precreated for you, but if you manually create it, simply by new App() (or even without a derived class, new System.Windows.Application()), everything in your project will see it.
You can create a common ResourceDictionary and add it to the Resources of your UserControls. That way you just have to change your Styles in one location.
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
and add it to your UserControl using MergedDictionarys
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
or just add it to the Control's Resources like this
<UserControl.Resources>
<ResourceDictionary Source="Dictionary1.xaml"/>
</UserControl.Resources>