How to re-use grid definitions from ResourceDictionary? - wpf

I'm trying to re-use a Grid which is defined under ResourceDictionary(guiLayout.xaml)
guiLayout.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Key="guiLayout">
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
and also I have MainWindow.xaml,
<Window x:Class="WpfApplication3.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>
<!-- I want to use ResourceDictionary definitions here-->
</Grid>
Is it possible to use the Grid definition from guiLayout.xml in Mainwindow.xaml?
Edit:
The project looks like this,

You can set the Content of the Window in XAML like this:
<Window x:Class="WpfApplication3.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"
Content="{StaticResource guiLayout}">
</Window>
Or using element syntax:
<Window x:Class="WpfApplication3.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.Content>
<StaticResource ResourceKey="guiLayout"/>
</Window.Content>
</Window>
Note that without using x:Shared for the Grid saved as Resource, that Grid is usable only for 1 window. To use it for multiple windows, you have to add x:Shared="false" for the Grid like this:
<Grid x:Key="guiLayout" x:Shared="false">
<!-- ... -->
</Grid>
Update: Try this code to import the resources:
In App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\guiLayout.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Related

Different style of progress bar in Mahapps

I don't know why Mahapps progress bar still has the same style despite the fact that I change style in App.xaml (other controls works fine).
Is there any other way than creating from zero a style for this control?
App.xaml
<Application x:Class="WpfApp2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Orange.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
MainWindow.xaml
<Controls:MetroWindow x:Class="WpfApp2.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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800"
Background="WhiteSmoke">
<StackPanel VerticalAlignment="Center">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200*"/>
</Grid.RowDefinitions>
<Controls:Tile Grid.Column="0" Grid.Row="0" Width="200" Height="200"/>
<Controls:Tile Grid.Column="1" Grid.Row="0" Width="200" Height="200"/>
<Controls:Tile Grid.Column="2" Grid.Row="0" Width="200" Height="200"/>
</Grid>
<Controls:MetroProgressBar Value="50" />
</StackPanel>

WPF I can not get MouseMove event when the form AllowsTransparency=true

<Window x:Class="mtWPFScratchPad.DeskForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="mtWPFScratchPad" AllowsTransparency="True" WindowState="Maximized" Loaded="Window_Loaded" WindowStyle="None" Background="#00FFFFFF" Closing="Window_Closing" Topmost="True" MouseMove="Window_MouseMove">
<InkCanvas Name="inkCanv" Background="Transparent">
</InkCanvas>
When i set the window should be transparency.I can not get the mousemove Event.
How to do this?
If you set Both transparent it's not working You have to change ink canvas color or either change Allow transparency = false
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" AllowsTransparency="True" WindowState="Normal Loaded="Window_Loaded_1" WindowStyle="None" Background="#00FFFFFF" Closing="Window_Closing_1" Topmost="True" MouseMove="Window_MouseMove_1" Height="350" Width="525" >
<Grid>
<InkCanvas Name="inkCanvas" Background="White">
</InkCanvas>
</Grid>
</Window>

How to add Context Menu in wpf in Modern Metro UI

I am doing one wpf app using Modern Metro UI,i want context menus on this app
as i tried
<Controls:MetroWindow x:Class="Something" 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:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title=""
Width="326.478"
Height="5" ShowIconOnTitleBar="True" ShowTitleBar="True"
WindowStartupLocation="CenterScreen"
GlowBrush="{DynamicResource AccentColorBrush}"
mc:Ignorable="d" ResizeMode="CanMinimize" Loaded="MetroWindow_Loaded" >
<Controls:MetroWindow.Resources>
<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="Send" />
</ContextMenu>
</Controls:MetroWindow.Resources>
<Grid>
</Grid>
</Controls:MetroWindow>
I am trying to add Context Menu to Wpf application which uses Modern Metro UI
A context menu should be declared within the actual element:
<Controls:MetroWindow x:Class="Something" 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:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title=""
Width="326.478"
Height="5" ShowIconOnTitleBar="True" ShowTitleBar="True"
WindowStartupLocation="CenterScreen"
GlowBrush="{DynamicResource AccentColorBrush}"
mc:Ignorable="d" ResizeMode="CanMinimize" Loaded="MetroWindow_Loaded" >
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Send" />
</ContextMenu>
</Grid.ContextMenu>
</Grid>

How to use PivotViewer in Silverlight xaml?

How to use PivotViewer in Silverlight xaml? I read http://msdn.microsoft.com/en-us/library/system.windows.controls.pivot.pivotviewer(v=vs.95).aspx and added a reference to the assembly.But I get an error
The name "PivotViewer" does not exist in the namespace "http://schemas.microsoft.com/client/2007".
This is my xaml:
<UserControl x:Class="SilverlightApplication2.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Hello world"></TextBlock>
<PivotViewer/>
</Grid>
</UserControl>
Added a reference to the assembly System.Windows.Controls.Pivot then
<UserControl x:Class="SilverlightApplication2.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:pivot="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Hello world"></TextBlock>
<pivot:PivotViewer x:Name="pViewer" />
</Grid>
</UserControl>
I have a series on my blog that walks thru using the PivotViewer. That should get you started. http://tonychampion.net/blog/index.php/series/pivotviewer-basics/

WPF how to include resource

So as I was reading how to use a MVVM design in my project looking into a sample-MVVM-concept project I found this:
<Window x:Class="MVVMDemo.UserRegistrationView"
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:viewmodel="clr-namespace:MVVMDemo"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<viewmodel:ViewModel x:Key="ViewModel"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
I create a new project add this:
xmlns:viewmodel="clr-namespace:MVVMDemo"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<viewmodel:ViewModel x:Key="ViewModel"/>
</Window.Resources>
and "DataContext="{Binding Source={StaticResource ViewModel}}"" to the Grid clause.
But I get strange errors. What is the problem? How do I fix it?

Resources