Posts Tagged ‘WPF’

Error when Trying to Run an WPF Program: XamlParseException occurred

August 4, 2011

PROBLEM:

image

This error is a very difficult one to identify.  It took me a couple of hours and remembering what code I added within the last compile.  Note:  This error does not occur when you compile, but when you run the application. 

What it turns out to be is when you write the following XAML code for a Window Form in WPF (Windows Presentation Foundation). 

The error was on Line number 14 but not line position 9… as the above error display shows.

Code Snippet
  1. <Window
  2.     x:Name="PullDraftDetail"
  3.     x:Class="PullDraftDetail"
  4.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  5.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  6.     AllowsTransparency="True"
  7.         Title=""
  8.          mc:Ignorable="d"
  9.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
  10.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  11.         d:DesignHeight="200" d:DesignWidth="700"
  12.         SizeToContent="WidthAndHeight"
  13.         WindowStartupLocation="CenterScreen"
  14.         WindowStyle="None" Background="{x:Null}" Icon="/PullDraft_WPF;component/images/Book_JournalwPen.ico"
  15.     >

This code produces the following form in “Design View”.
image 

What I wanted is to make the Window transparent and display the “GroupBox" and all the other elements by setting the Window’s “Background=”{x:Null}”

But, when I added the “Icon=”/PullDraft_WPF;component/images/Book_JournalwPen.ico” I got the error “XamlParseException occurred”.  By the way… I did not need the “Icon” element because the Window was transparent anyway.

Solution:

I removed the Icon element and everything ran fine. 

Code Snippet
  1. <Window
  2.     x:Name="PullDraftDetail"
  3.     x:Class="PullDraftDetail"
  4.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  5.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  6.     AllowsTransparency="True"
  7.         Title=""
  8.          mc:Ignorable="d"
  9.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
  10.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  11.         d:DesignHeight="200" d:DesignWidth="700"
  12.         SizeToContent="WidthAndHeight"
  13.         WindowStartupLocation="CenterScreen"
  14.         WindowStyle="None"
  15.         Background="{x:Null}"
  16.         >

…End of Line…

How to: Use UserControls in WPF XAML Windows

February 28, 2011

I created a UserControl called: “StatusBarControl” and it has a StatusBar contol with the following contols:  UserNameTextBlock, a  Lable for reporting progress, a ComboBox for displaying Styles of the form so the user can change them using the my.settings.UserStyle string,  and a StatusBar for showing progress. 

You first have to put in the header of the XAML Window the Following

In order to use a local UserControl you have to define the NameSpace of the Project that the Control resides in.

xmls:local=”clr-namespace:MyProjectWPF_GUI

Code Snippet
  1. <Window x:Class="MyProjectWindow"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.     WindowStartupLocation="CenterOwner"
  5.     ResizeMode="NoResize"
  6.     Title="My Project" Height="480" Width="647"
  7.       xmlns:local="clr-namespace:MyProjectWPF_GUI"  
  8.     Name="MyProject">

 

Next you create the UserControl as a UserControl in your project.  My UserContol is called “StatusBarControl”.  Then you assign it its local name.

<local:StatusBarControl x:Name=”StatusBarControl…  />

Code Snippet
  1.         <StackPanel VerticalAlignment="Top" Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2" Height="26" Margin="0,4,0,0" Name="StackPanel3" Width="526">
  2.             <Button Name="GetSmartIndexButton" Width="132" Height="23" ToolTip="You must get Smart Index Data before you can Submit to Mobius">Get Smart Index Data</Button>
  3.             <Button Name="ClearFieldsButton" Margin="3,0,0,0" Width="82" Height="23" ToolTip="Clears all Fields on this Tab.">Clear Fields</Button>
  4.             <Button Name="SubmitButton" Margin="3,0,0,0" Height="23" Width="116" ClickMode="Release" ToolTip="Submit the images to Mobius">Submit to Mobius</Button >
  5.         </StackPanel>
  6.             
  7.         <!–<Rating Height="100" HorizontalAlignment="Right" Margin="0,0,-285.057,-48.343" Name="Rating1" VerticalAlignment="Bottom" Width="200" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" />–>
  8.         <local:StatusBarControl x:Name="StatusBarControl" HorizontalAlignment="Left" Grid.Row="3" Height="30" VerticalAlignment="Bottom" Grid.ColumnSpan="5" Width="655" Margin="-12,0,0,-5" />
  9.  
  10.     </Grid>
  11. </Window>

 

It reference the UserControl File called “StatusBarControl.xaml” under a directory “UserControls” in the Project “MyProjectWPF_GUI”

StatusBarControl.xaml

Code Snippet
  1. <UserControl x:Class="StatusBarControl"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
  6.              mc:Ignorable="d"
  7.              d:DesignHeight="25" d:DesignWidth="650">
  8.     <Grid Height="31" Width="609">
  9.         <Grid Height="25" VerticalAlignment="Top">
  10.             <StatusBar Height="25" Name="statusBar1" VerticalAlignment="Top" Margin="-20,0,-41,0" />
  11.             <Label Content="Progress" FlowDirection="RightToLeft" Margin="0,0,41,0" Name="ProgressLabel" HorizontalAlignment="Right" Width="390" Visibility="Hidden" Height="25" VerticalAlignment="Top" />
  12.             <!–</StackPanel>–>
  13.         </Grid>
  14.         <StackPanel Orientation="Horizontal" Height="25" Name="StackPanel1" Margin="0,1,433,0" VerticalAlignment="Top">
  15.             <ComboBox DataContext="{Binding}" Height="23" ItemsSource="{Binding}" Name="StylesComboBox" Text="Hello" Visibility="Collapsed" Width="121" />
  16.             <Label Content="User: " Height="24" Name="UserNameTextBlock" Width="50" />
  17.         </StackPanel>
  18.     </Grid>
  19. </UserControl>

 

To update/refresj  the “StatusBarControl” you will need the Application.DoEvents that is in the System.Windows.Forms NameSpace…  System.Windows.Forms.Application.DoEvents()

Code Snippet
  1.     'In order to get the following code to update the color and the cursor you have to call the System.Windows.Forms.Application.DoEvents()
  2.         Me.Background = New SolidColorBrush(My.Settings.WaitFormColor)
  3.         Me.Cursor = Cursors.Wait
  4.         'Application.DoEvents()
  5.         System.Windows.Forms.Application.DoEvents()

 

That is it.  If you have any Questions, please leave me a comment.

How To: Use Windows.Resources and Resource.Dictionary for Setting Default Styles for ToolTip and Button Controls

February 28, 2011

 

<Window.Resources/>

In the XAML file you are working on add the following code if the Resource.Dictionary is named StylesDictionary.xaml and is located in a root directory called Styles.  The Project name is called MyProjectWPF_GUI.

Code Snippet
  1.  
  2. <Window.Resources>
  3.     <ResourceDictionary>
  4.         <ResourceDictionary.MergedDictionaries>
  5.             <ResourceDictionary Source="pack://application:,,,/MYProjectWPF_GUI;component/Styles/StylesDictionary.xaml" />
  6.         </ResourceDictionary.MergedDictionaries>
  7.     </ResourceDictionary>
  8. </Window.Resources>

 

Note:

The Source  = pack://application:,,,/MyProjectWPF_GUI;component/Styles/StylesDictionary.xaml”

needs to have this “pack” syntax if you have a namespace declaration for “UserControls” like is used in this Window.

xmlns:local="clr-namespace:MyProjectWPF_GUI" 

The error at runtime will be: “Set property ‘System.Windows.ResourceDictionary.Source’ threw an exception.”

 

Otherwise if you do not declare a namespace for a “UserControl” then you can call it by

Source=”/Styles/StylesDictionary.xaml”

Code Snippet
  1. <Window x:Class="MobiusWindow"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.     WindowStartupLocation="CenterOwner"
  5.     ResizeMode="NoResize"
  6.     Title="Submit to Mobius" Height="480" Width="647"
  7.       xmlns:local="clr-namespace:MYProjectWPF_GUI"  
  8.     Name="MobiusWin">

 

<Resource.Dictionary/>

The Resource.Dictionary looks like the following.  Its filename is called StylesDictionary.xaml and is under a directory called Styles.

Code Snippet
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  2.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  3.  
  4.     <!– Default Button Style–>
  5.           <Style TargetType="Button">
  6.             <Setter Property="FontWeight" Value="Bold" />
  7.         </Style>
  8.     
  9.     <!– Default ToolTip tyle–>
  10.         <Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
  11.             <Setter Property="OverridesDefaultStyle" Value="true"/>
  12.             <Setter Property="HasDropShadow" Value="True"/>
  13.             <Setter Property="Template">
  14.                 <Setter.Value>
  15.                     <ControlTemplate TargetType="ToolTip">
  16.                         <Border CornerRadius="7" HorizontalAlignment="Center" VerticalAlignment="Top" Padding="5" BorderThickness="3,3,3,3">
  17.                             <Border.Background>
  18.                                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  19.                                     <GradientStop Color="#CF181818" Offset="0"/>
  20.                                     <GradientStop Color="#BE1C1C1C" Offset="1"/>
  21.                                 </LinearGradientBrush>
  22.                             </Border.Background>
  23.                             <Border.BorderBrush>
  24.                                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  25.                                     <GradientStop Color="#80FFFFFF" Offset="0"/>
  26.                                     <GradientStop Color="#7FFFFFFF" Offset="1"/>
  27.                                     <GradientStop Color="#FFFFF18D" Offset="0.344"/>
  28.                                     <GradientStop Color="#FFFFF4AB" Offset="0.647"/>
  29.                                 </LinearGradientBrush>
  30.                             </Border.BorderBrush>
  31.                             <StackPanel>
  32.                                 <TextBlock FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="#FFFFFFFF" />
  33.                             </StackPanel>
  34.                         </Border>
  35.                     </ControlTemplate>
  36.                 </Setter.Value>
  37.             </Setter>
  38.         </Style>
  39. </ResourceDictionary>

 

Good Luck

WPF ToolTips

February 25, 2011

Code Snippet
  1. <Button Content="Submit"> <Button.ToolTip> <ToolTip> <StackPanel> <TextBlock FontWeight="Bold">Submit Request</TextBlock> <TextBlock>Submits the request to the server.</TextBlock> </StackPanel> </ToolTip> </Button.ToolTip> </Button>

Creating a ToolTip for a Button

Code Snippet
  1. <Button Content="Submit"> <Button.ToolTip> <ToolTip> <StackPanel> <TextBlock FontWeight="Bold">Submit Request</TextBlock> <TextBlock>Submits the request to the server.</TextBlock> </StackPanel> </ToolTip> </Button.ToolTip> </Button>

Code Snippet
  1. <Button IsEnabled="False" ToolTip="Saves the current document" ToolTipService.ShowOnDisabled="True" Content="Save"> </Button>

How to show ToolTips on Disabled Controls

Code Snippet
  1. <Button IsEnabled="False" ToolTip="Saves the current document" ToolTipService.ShowOnDisabled="True" Content="Save"> </Button>

 

How to Change the Show Duration of a ToolTip

Code Snippet
  1. <Button ToolTip="Saves the current document" ToolTipService.ShowDuration="20" Content="Save"> </Button>

Video Training Sites (Steven K James’s recommendations)

December 22, 2010

Here are the sites that I use for Training and Certification.  Some are Free and some cost a Subscription price.  Please contact me if you are interested in the subscription site purchases.  I am a reseller of them.

 

FREE Training Sites

Dr Dobb’s TV

 

Channel 9 Videos about the people building Microsoft Products & Services

 

Subscription Training Sites (Contact me for pricing.  I am a vendor and user of these sites.)

.NET Tutorial Videos from Beginner to Expert  LearnVisualStudio.NET

 

Microsoft Training at AppDev IT Training, Developer Training, Microsoft Certification Training,

 

Contact Information:  Steven.James@1800thenerd.com

Or call: 1-800-The-Nerd