WPF include resource directory XamlParserException - c#

I have a userControl like this:
<UserControl x:Class="LoginModule.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:UserControls;assembly=UserControls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/UserControls;component/Styles.xaml" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<Label Grid.Row="1" Grid.Column="0" Name="labelLogin" VerticalAlignment="Center">Login:</Label>
<Label Grid.Row="2" Grid.Column="0" Name="labelPassword" VerticalAlignment="Center">Password:</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="textboxLogin" VerticalAlignment="Center"></TextBox>
<TextBox Grid.Row="2" Grid.Column="1" Name="textboxPassword" VerticalAlignment="Center"></TextBox>
<Button Grid.Row="3" Grid.ColumnSpan="2" Template="{StaticResource SilverButton}" Height="25" Width="200" Name="buttonLogin" Content="Log In" Click="buttonLogin_Click" />
</Grid>
</UserControl>
In designer mode everything is ok (style works), all solution building with success. But when I run program with debug I get XamlParserException in:
<ResourceDictionary Source="pack://application:,,,/UserControls;component/Styles.xaml" />
with message like: Could not load UserControl oir one of his elements. Could not load file.
I have reference to UserControl.dll I don't know what's going on.
Thanks for any fast help.
Kamilos

I think this is because you need to use merged dictionaries:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/YourResource.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

Related

Visual Studio Error-XLS0509 - how to fix it? - XAML - Microsoft Tutorial

I was following the tutorial from - https://learn.microsoft.com/en-us/dotnet/desktop/wpf/getting-started/walkthrough-my-first-wpf-desktop-application?view=netframeworkdesktop-4.8
till the handle events section
But I encountered at line 35 in this XAML code -ERROR XLS0509
Saying
Error XLS0509 Property elements cannot be in the middle of an element's content. They must be before or after the content.
<Page x:Class="ExpenseIt.ExpenseItHome"
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:local="clr-namespace:ExpenseIt"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="500"
Title="ExpenseIt - Home">
<Grid Margin="10, 0, 10, 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!--People List-->
<Label Grid.Column="1" VerticalAlignment="Center" FontFamily="Trebuchet MS" FontWeight="Bold" FontSize="18" Foreground="White">
View Expense Report
</Label>
<Border Grid.Column="1" Grid.Row="1" Height="35" Padding="5" Background="#FFD44E4E">
<Label VerticalAlignment="Center" Foreground="White">Names</Label>
</Border>
<!-- bg img-->
<Grid.Background>
<ImageBrush ImageSource="368.png"/>
</Grid.Background>
<ListBox Grid.Column="1" Grid.Row="2">
<ListBoxItem>Mike</ListBoxItem>
<ListBoxItem>Lisa</ListBoxItem>
<ListBoxItem>John</ListBoxItem>
<ListBoxItem>Mary</ListBoxItem>
</ListBox>
<!-- View report Button -->
<Button Grid.Column="1" Grid.Row="3" Margin="0, 10, 0, 0" Width="125" Height="25" HorizontalAlignment="Right">View</Button>
</Grid>

When i want to get a value out of a combo-box and put it as a Stroke of a Ellipse my program crashes

The Error Page says: "The application is in break Mode"
Getting the Color of the ColorPicker and using it as Background for the Ellipse is working just fine.
My XAML Code:
.
<Window x:Class="FirstApp.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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:FirstApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Column="0" Grid.Row="0" Text="Neuer Name" Name="txtName"/>
<Button Grid.Column="1" Grid.Row="0" Content="Kopieren" Name="btnKopieren" Click="btnKopieren_Click"
/>
<Slider Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Name="slRadius"
Maximum="200" />
<Ellipse Grid.Column="0" Grid.Row="2"
Fill="{Binding ElementName=colorPicker, Path=HexadecimalString}"
Stroke="{Binding ElementName=lstFarben, Path=SelectedValue}"
Height="{Binding ElementName=slRadius, Path=Value}"
Width="{Binding ElementName=slRadius, Path=Value}"/>
<StackPanel Grid.Column="1" Grid.Row="2" >
<ComboBox x:Name="lstFarben">
<ComboBoxItem Name="cbi1" Content="#0061ff" />
<ComboBoxItem Name="cbi2" Content="#00ff04" />
<ComboBoxItem Name="cbi3" Content="#ff0019" />
</ComboBox>
<xctk:ColorCanvas Name="colorPicker" />
</StackPanel>
<Label Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2"
Content="{Binding ElementName=lstFarben, Path=SelectedValue}" Name="lblNameGross"/>
</Grid>
Here a Screenshot of the Error:
Screenshot
Since you are explicitly creating ComboBoxItems, the color value has to be retrieved from their Content property.
Set the SelectedValuePath property of the ComboBox:
<ComboBox x:Name="lstFarben" SelectedValuePath="Content">
Alternatively, get the color value directly from the selected item like this:
Stroke="{Binding ElementName=lstFarben, Path=SelectedItem.Content}"

Material Design Style PackIcon

I created with Material Design a UserControl Button, based on the method behind I would like to reuse the template with change of the icon. I tried to add the materialdesign:Packicon into the UserControl.Resources, but seems wrong. The Attribute Style is already in use. How can I achieve my icon change?
<UserControl x:Class="MaterialDesignTest1.UserControl2"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d" d:DesignWidth="300" Height="132">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid Height="132" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Header -->
<Button Grid.Row="0" Grid.Column="0" Background="WhiteSmoke" BorderBrush="LightGray" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}"
HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1,1,1,1" Width="50" Height="50">
<materialDesign:PackIcon Height="30" Width="30" Kind="BluetoothConnect" />
</Button>
</Grid>
in code behind, create a new packicon, set the content of the button like below:
PackIcon packIcon = new PackIcon();
packIcon.Kind = PackIconKind.FullscreenExit;
btnResizeDashboard.Content = packIcon;

Using same dictionary template multiple times?

I'm trying to make sudoku using a 9x9 grid however in order to have clean code i'm trying out dictionaries. After writing the code given below the preview achieves the 9x9 grid. However whenever I launch the application I don't get the same result I only get the last loaded grid.
this is the dictionary that has the following grid element:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Key="GridTemplate" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" ></Label>
<Label Grid.Column="0" Grid.Row="1" ></Label>
<Label Grid.Column="0" Grid.Row="2" ></Label>
<Label Grid.Column="1" Grid.Row="0" ></Label>
<Label Grid.Column="1" Grid.Row="1" ></Label>
<Label Grid.Column="1" Grid.Row="2" ></Label>
<Label Grid.Column="2" Grid.Row="0" ></Label>
<Label Grid.Column="2" Grid.Row="1" ></Label>
<Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>
Now i'm trying to use this 9 times into another grid into this window
<Window x:Class="SudokuWPF.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>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GridDictonary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="0" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="1" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="2" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="0" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="1" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="2" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="0" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="1" />
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="2" />
</Grid>
I"m hoping there is a fix for this problem cause i'm out of options :(
It might be helpful to wrap the resource in a user control instead?
This worked on my machine (sans populating the numbers)
Page:
<Page x:Class="WPFAnswers.Ans34384501.P1"
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:local="clr-namespace:WPFAnswers.Ans34384501"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="P1">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<local:UC1 Grid.Column="0" Grid.Row="0"></local:UC1>
<local:UC1 Grid.Column="0" Grid.Row="1"></local:UC1>
<local:UC1 Grid.Column="0" Grid.Row="2"></local:UC1>
<local:UC1 Grid.Column="1" Grid.Row="0"></local:UC1>
<local:UC1 Grid.Column="1" Grid.Row="1"></local:UC1>
<local:UC1 Grid.Column="1" Grid.Row="2"></local:UC1>
<local:UC1 Grid.Column="2" Grid.Row="0"></local:UC1>
<local:UC1 Grid.Column="2" Grid.Row="1"></local:UC1>
<local:UC1 Grid.Column="2" Grid.Row="2"></local:UC1>
</Grid>
User Control:
<UserControl x:Class="WPFAnswers.Ans34384501.UC1"
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:local="clr-namespace:WPFAnswers.Ans34384501"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Control.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GridDictonary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Control.Resources>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="0" />
</Grid>
Resource:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFAnswers.Ans34384501">
<Grid x:Key="GridTemplate" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" ></Label>
<Label Grid.Column="0" Grid.Row="1" ></Label>
<Label Grid.Column="0" Grid.Row="2" ></Label>
<Label Grid.Column="1" Grid.Row="0" ></Label>
<Label Grid.Column="1" Grid.Row="1" ></Label>
<Label Grid.Column="1" Grid.Row="2" ></Label>
<Label Grid.Column="2" Grid.Row="0" ></Label>
<Label Grid.Column="2" Grid.Row="1" ></Label>
<Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>
I am not really sure but I think this happens because you can't add the same Instance as a child of 2 or more elements, in this case every ContentControl is adding your grid as a child and that is not valid, you should try a DataTemplate Instead.
<DataTemplate x:Key="GridTemplate">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" ></Label>
<Label Grid.Column="0" Grid.Row="1" ></Label>
<Label Grid.Column="0" Grid.Row="2" ></Label>
<Label Grid.Column="1" Grid.Row="0" ></Label>
<Label Grid.Column="1" Grid.Row="1" ></Label>
<Label Grid.Column="1" Grid.Row="2" ></Label>
<Label Grid.Column="2" Grid.Row="0" ></Label>
<Label Grid.Column="2" Grid.Row="1" ></Label>
<Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>
</DataTemplate>

Maximizing a WPF Page and fitting contents

How do I maximize a WPF window without the contents getting all messed up?
I want to design it to start maximized and the contents to fit as I designed the form?
I read about setting the hight and width settings with a *, but I'm not too sure on how to apply it, as I have tried and it doesn't seem to work.
The code in the answer below works, but how do i use that for my own app??? What is it that makes the buttons and labels etc auto-resizing?
This functionality in your application you can achieve by using Grid (msdn)
Here is simple example:
<Window x:Class="AppMaximize.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 Height="30" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Row1Col1" />
<Button Content="Row1Col1" Grid.Row="3" Grid.Column="1" />
<Label Background="Red" Grid.Row="1" />
<Label Background="Green" Grid.Row="1" Grid.Column="1" />
<Label Background="Yellow" Grid.Row="2" />
<Label Background="Blue" Grid.Row="2" Grid.Column="1" />
</Grid>
</Window>

Categories