Adding window resources to a page? - wpf

I have created a window and set its resources as:
<Window.Resources>
<ResourceDictionary Source="Styles/StyleDark.xaml"/>
</Window.Resources>
I wanted to add a page to the window. I have added the page through the
<Frame Source="LoginScreen.xaml"/>
The login Screen uses the resources i have added in the window resources.But when i run, the stylesheet is not applied.
I have tried to copy paste the content of page and it works fine. What mistake am I making?

Related

Show window icon in title bar with MahApps.Metro?

I started using MahApps.Metro just yesterday, and I can't figure out how to get application icon to show as window icon on a window (or better, all of them). Here in the example window icon is included, but I can't seem to get it. When I use
ShowIconOnTitleBar="True"
it doesn't do anything.
What am I doing wrong?
You need to set the Iconproperty like this example (taken from the Mahapps demo app) :
<Controls:MetroWindow x:Class="MetroDemo.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="MahApps.Metro - Demo Application"
Width="960" Height="600"
Icon="mahapps.metro.logo2.ico"
ShowIconOnTitleBar="True"
ShowTitleBar="True">
<Grid />
</Controls:MetroWindow>
I suggest that you download the source code for the Mahapps project, which also contains the source for a demo app (too bad they don't advertise it on their website), it's great for examples. You can find it on GitHub here

Modal effect for multi-level windows in WPF

Salutations,
I have been unable to find a way to apply a "fading" or "darkening" to my application when a modal window appears. I have multiple windows, so lets say the application as window1, can launch window2, which can launch a modal window3. When I try to apply any effects, I can only seem to be able to darken (or blur or whatever else I feel like) the immediate parent (window2), or the entire monitor space (which I do not want).
How can I apply an effect to all windows of the application, leaving only the current active/modal one clear?
You could add an object (boolean?) to the resources of the App object and bind an overlay that you put in each window.
Bind the Visibility and IsEnabled properties of the Window to prevent the user tabbing to the controls underneath the overlay.
When you open or close a Modal dialog, toggle the object.
<Application x:Class="WpfApplication1.App"
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"
StartupUri="MainWindow.xaml">
<Application.Resources>
<sys:Boolean x:Key="IsShowingModalDialog">false</sys:Boolean>
</Application.Resources>
</Application>

Why are my WPF styles not shown in the designer?

I've created a WPF application using Visual Studio 2010, converted App.xaml to a Page and added a call to InitializeComponent in the constructor. I've then created a new window called "LoginWindow" and added the following to the App.xaml.cs:
[STAThread]
public static void Main()
{
var app = new App();
app.Run(new LoginWindow());
}
Next I added a style to the App.xaml as follows:
<Style x:Key="MyWindowStyle" TargetType="Window">
<Setter Property="Background" Value="Red" />
</Style>
Finally, in the LoginWindow I added the following style reference:
Style="{StaticResource MyWindowStyle}"
When I run the program I see my login window with a red background as expected. However, when I view the window in the designer the style is not applied. The {StaticResource MyWindowStyle} is underline and showing the error, "The resource 'MyWindowStyle' could not be resolved".
Why is this?
EDIT
I got a fix on another question that sorted this one out too. I stopped the app.xaml being a page and used the StartUp even instead of a Main method.
I'm not sure about the reason for your designer problem, but my suggestion would be to go back to the default WPF application template and see if that works.
App.xaml is an important file and shouldn't be converted to a page (you should add a separate page), and you don't need your Main method to start the application: in the default app.xaml file you'll see an attribute that (in a default project) is StartupUri="MainWindow.xaml" - use that to point to your LoginWindow.xaml.
I've just tried it in VS using the standard template just to check, and I see no problem. To confirm, all I did was add your style to the (default) app.xaml file, and apply it to my window in the same way as you - it shows fine in the designer.
The reason that it doesn't show up in the designer is that it only looks in the UserControl/whatever + Any Application named App's XAML. Since you made App.Xaml into a Page the designer cannot know that that is the one the UserControl will be hosted within. At run-time WPF will look in the Page and any Parent it might have (including Parent's Parent and so on).
Instead, Let App.xaml point to a 'dummy'-page that in it's overriden Loaded-event navigates to the right Page with the DataContext set.

Changing the width of Window when using pages in WPF

Im using pages in the WPF project that im currently working on. However i can't seem to figure out how to change the width of a page, or rather, the width of the window that hosts the pages?
Setting the page width property only changes the width of the page inside the window frame.
Setting the with of the mainwindow or navigationwindow through:
<Application.MainWindow>
<Window Width="400" />
</Application.MainWindow>
<Application.MainWindow>
<NavigationWindow Width="400" />
</Application.MainWindow>
Doesnt work either. So how do i set the width of the window in XAML?
It is indeed a pain: you need a NavigationWindow that can navigate to the page. As this inherits from Window you can set the Height and Width on this container.
-Open a new wpf aplication
-delete the standard window1 you get.
Change the App.xaml thus (delete the StartupUri attribute):
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
Write the App.xaml.cs thus:
public partial class App : Application
{
private NavigationWindow navigationWindow;
private void Application_Startup(object sender, StartupEventArgs e)
{
navigationWindow = new NavigationWindow();
navigationWindow.Height = 200;
navigationWindow.Width = 100;
var page = new Page1();
navigationWindow.Navigate(page);
navigationWindow.Show();
}
you can add a page from the project menu. This will give you something like:
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<Grid>
<TextBlock>test</TextBlock>
</Grid>
</Page>
Good luck!
If you're just asking how to set the dimensions of the Window itself, then just open up the Window's XAML file and set the Width/Height properties:
Window Width="640" Height="480"
If you actually want a diff. Window size per Page, you need to do some more work. The available real-estate for a Page is controlled by the host Window. There is no intrinsic way for a Page to ask for more real estate from the host, but you could build support into your iwn app by creating some attached propertirs which your host window knows about and can be applied by the Page author. When the page loads your host can check to see if these properties are set and adjust its own width accordingly.

How do I embed one WPF form into another?

I would like to have master WPF form with tab control where each tab contains one independent WPF form. Those forms do not depend on each other, so I thought it will be easier to develop each of them separately and then just embed them into master form.
Number of forms is known, so there is no need for dynamic plugin system.
When you use a Frame or NavigationWindow you can have it load different xaml Pages and even html. You can also make it act like a browser with forward and backward navigation. See http://msdn.microsoft.com/en-us/library/ms750478.aspx
You could put a Frame on each tab and have it load a certain Page.
<Window x:Class="PluginApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Frame Name="frame" NavigationUIVisibility="Visible" Source="SomePage.xaml" />
</DockPanel>
</Window>
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Page Title"
WindowWidth="500"
WindowHeight="200">
Hello world
</Page>
Make the child forms derive from UserControl, in the master form add a tab control with one of those user controls inside each tab.
I prefer make this with userControl
First create User control
after, include reference of this userControle in another place
anywhere in the frmHome...
<DockPanel>
<Entity:ucContactList/>
</DockPanel>

Resources