WPF how to include resource - wpf

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?

Related

Binding Window1 Icon to MainWindow Icon

I tried following code but it doesnt work. Any suggestions?
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1" x:Name="Window1" Title="Window1"
Height="300" Width="300"
Icon="{Binding ElementName=Application.Current.MainWindow, Path=Icon}" >
<Grid>
</Grid>
This should work:
<Window ...
Icon="{Binding Path=MainWindow.Icon, Source={x:Static Application.Current}}">
<Grid>
</Grid>
</Window>

How to re-use grid definitions from ResourceDictionary?

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>

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/

Visual Studio 2012 XAML Designer - Cannot add more than one item

I'm new to VS 2012, and I have this issue every time I use the XAML Designer.
Every time I add an item (e.g. a RadioButton, and Image, a Label) to my window, it deletes the previous one.
As a result, I can have only one item in my window, I know it is absurd, what am I missing?
Here is the xaml of the window
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="400" Width="600">
<Toolkit:KinectSensorChooserUI x:Name="SensorChooserUI" VerticalAlignment="Center" Height="40" Margin="277,2,275,328"/>
</Window>
Here is the xaml of the window after dragging an image on it (from the Toolbox)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="400" Width="600">
<Image HorizontalAlignment="Left" Height="86" Margin="77,188,0,0" VerticalAlignment="Top" Width="111"/>
</Window>
As made clear by #Hans, I was trying to add multiple content items into a Window, in the XAML designer. This is just not possible so i had to:
1) Add a Grid to the Window.
2) Add any item to the grid.
It works, here is a sample code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="768" Width="1024" Loaded="Window_Loaded_1">
<Grid HorizontalAlignment="Left" Height="736" VerticalAlignment="Top" Width="1012" Margin="2,2,0,0">
<Image x:Name="Image01" HorizontalAlignment="Left" Height="240" Margin="136,27,0,0" VerticalAlignment="Top" Width="320"/>
<TextBlock x:Name="tbMessages" HorizontalAlignment="Left" Height="60" Margin="10,606,-664,-426" TextWrapping="Wrap" VerticalAlignment="Top" Width="974"/>
<WpfViewers:KinectColorViewer HorizontalAlignment="Left" Height="240" Margin="666,0,-666,0" VerticalAlignment="Top" Width="320"/>
</Grid>
</Window>

Resources