WPF-Project System.Windows.Markup.XamlParseException - wpf

I hope someone can help me.
I have a WPF-Project (it´s working fine) and i want use it
in another one. But i got a System.Windows.Markup.XamlParseException.
at:
<Expander IsExpanded="True"
Header="Symbols"
Margin="0,1,0,0"
Content="{StaticResource SymbolStencils}" />
my SymbolStencils looks like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:DiagramDesigner">
<s:Toolbox x:Key="SymbolStencils"
DefaultItemSize="55,55">
<ItemsControl.Items>
<Button IsHitTestVisible="False">Button</Button>
<ComboBox IsHitTestVisible="False" />
<Label IsHitTestVisible="False" >Label</Label>
<ListBox IsHitTestVisible="False" />
<Rectangle IsHitTestVisible="False" />
<TextBox IsHitTestVisible="False" >Text</TextBox>
<TextBlock IsHitTestVisible="False" >Text</TextBlock>
<RadioButton IsHitTestVisible="False" />
<CheckBox IsHitTestVisible="False" />
</ItemsControl.Items>
</s:Toolbox>

<Window x:Class="DiagramDesigner.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:DiagramDesigner"
WindowStartupLocation="CenterScreen"
Title="Diagram Designer Part 2"
FontFamily="SegoeUI"
Height="850"
Width="1100">
Thats my head in the XAML file

Related

WPF Combobox SelectionChanged Event is not firing

I have a comboBox, user controls and a button. SelectionChanged event is not firing in my UI App but when I copy the same combobox code in a different WPF app, selectionChanged event is firing. I don't understand why event is not triggered just here.
<Window x:Class="ExecutionInformation.TesterApp.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:local="clr-namespace:ExecutionInformation.TesterApp.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="800">
<ScrollViewer>
<Grid>
<StackPanel>
<Label Content="Execution Information service" Margin="20,10,20,10"
HorizontalAlignment="Center" FontSize="15" FontFamily="Bold"/>
</StackPanel>
<StackPanel Margin="20">
<!--<ComboBox Name="CmbAPIList" ItemsSource="{StaticResource ExecInfoServiceApis}"
DisplayMemberPath ="ValueString"
SelectedValuePath="ValueString" Margin="100,20,100,0"
SelectedValue="{Binding ColorString}" SelectionChanged="CmbAPIList_SelectionChanged"/>-->
<ComboBox Name="CmbAPIList"
SelectionChanged="CmbAPIList_SelectionChanged">
<ComboBoxItem>ApproveTestStartAsync</ComboBoxItem>
<ComboBoxItem>CompleteTestAsync</ComboBoxItem>
<ComboBoxItem>GetPartAsync</ComboBoxItem>
<ComboBoxItem>GetAssemblyAsync</ComboBoxItem>
<ComboBoxItem>PutAssemblyAttributesAsync</ComboBoxItem>
</ComboBox>
<local:ApproveTestStartUC x:Name="approveTestStartUC" Visibility="Collapsed" Margin="20,10,20,10" Background="Cyan" />
<local:CompleteTestUC x:Name="completeTestUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetPartUC x:Name="getPartUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetAssembly x:Name="getAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:PutAssemblyAttributesUC x:Name="putAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10"/>
<WrapPanel Margin="15" HorizontalAlignment="Center">
<Button Name="btnShowResults" Click="btnShowResults_Click" Visibility="Collapsed" Width="100">Show Results</Button>
</WrapPanel>
<Border BorderThickness="1" BorderBrush="LightGreen">
<TextBlock Name="TxtResults" Visibility="Collapsed"></TextBlock>
</Border>
</StackPanel>
</Grid>
</ScrollViewer>
Commented ComboBox code in the above code works without any issues.
Thanks

Resize my user controls content with change in size of window or document panel

I have few buttons on click, they display a user control with images. when i resize any one of them, the images starts hiding. but i want them to expand and shrink with resize. please tell me how to achieve this.
OtherPanelView.xaml
<UserControl x:Class="DXDockingMVVMTest1.Views.OtherPanelView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="Orange">
<Grid>
<Image HorizontalAlignment="Left" Height="23" Margin="96,104,0,0" TextWrapping="Wrap" Source="{Binding Image}" VerticalAlignment="Top" Width="120"/>
</Grid>
</UserControl>
PanelView.xaml
<UserControl x:Class="DXDockingMVVMTest1.Views.PanelView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FF47DAB4">
<Grid>
<Image HorizontalAlignment="Left" Height="23" Margin="96,104,0,0" TextWrapping="Wrap" Source="{Binding Image}" VerticalAlignment="Top" Width="120"/>
</Grid>
</UserControl>
MainView.xaml
<Window x:Class="DXDockingMVVMTest1.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxd="http://schemas.devexpress.com/winfx/2008/xaml/docking"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:ViewModels="clr-namespace:DXDockingMVVMTest1.ViewModels"
xmlns:Views="clr-namespace:DXDockingMVVMTest1.Views" Title="Main Window" Height="400" Width="800">
<Window.Resources>
<!--<Views:Conv x:Key="conv"/>-->
<DataTemplate DataType="{x:Type ViewModels:PanelViewModel}">
<Views:PanelView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:OtherPanelViewModel}">
<Views:OtherPanelView/>
</DataTemplate>
<Style TargetType="dxd:DocumentPanel">
<Setter Property="Caption" Value="{Binding Caption}" />
<Setter Property="Views:Helper.Name" Value="{Binding Caption}"/>
<Setter Property="AllowFloat" Value="{Binding CanFloat}"/>
</Style>
</Window.Resources>
<dxb:BarManager>
<dxb:BarManager.Categories>
<dxb:BarManagerCategory Name="File" />
</dxb:BarManager.Categories>
<dxb:BarManager.Items>
<dxb:BarButtonItem Name="bNew" CategoryName="File" Content="Add Panel" KeyGesture="Ctrl+N" Hint="" Command="{Binding AddPanelCommand}"/>
<dxb:BarButtonItem Name="bOpen" CategoryName="File" Content="Add Other Panel" KeyGesture="Ctrl+O" Hint="Open" Command="{Binding AddOtherPanelCommand}"/>
<dxb:BarButtonItem Name="bSave" CategoryName="File" Content="Save" KeyGesture="Ctrl+S" Hint="Save Document" ItemClick="OnSaveLayout" />
<dxb:BarButtonItem Name="bExit" CategoryName="File" Content="Restore" KeyGesture="Ctrl+X" ItemClick="OnRestoreLayout"/>
</dxb:BarManager.Items>
<dxb:BarManager.Bars>
<dxb:Bar Caption="Panel1Bar" x:Name="Bar1" UseWholeRow="True" ShowDragWidget="False" AllowQuickCustomization="False">
<dxb:Bar.DockInfo>
<dxb:BarDockInfo ContainerName="panel1DockControl" />
</dxb:Bar.DockInfo>
<dxb:Bar.ItemLinks>
<dxb:BarButtonItemLink BarItemName="bNew" />
<dxb:BarButtonItemLink BarItemName="bOpen" />
<dxb:BarButtonItemLink BarItemName="bSave" />
<dxb:BarButtonItemLink BarItemName="bExit" />
</dxb:Bar.ItemLinks>
</dxb:Bar>
<dxb:Bar Caption="Panel2Bar" x:Name="Bar2" UseWholeRow="True" ShowDragWidget="False" AllowQuickCustomization="False">
<dxb:Bar.DockInfo>
<dxb:BarDockInfo ContainerName="{x:Null}" />
</dxb:Bar.DockInfo>
<dxb:Bar.ItemLinks>
<dxb:BarButtonItemLink BarItemName="bNew" />
<dxb:BarItemLinkSeparator/>
<dxb:BarButtonItemLink BarItemName="bOpen" />
<dxb:BarItemLinkSeparator/>
<dxb:BarButtonItemLink BarItemName="bSave" />
<dxb:BarItemLinkSeparator/>
<dxb:BarButtonItemLink BarItemName="bExit" />
</dxb:Bar.ItemLinks>
</dxb:Bar>
</dxb:BarManager.Bars>
<DockPanel>
<dxb:BarContainerControl Name="Top_Container" DockPanel.Dock="Top"/>
<dxd:DockLayoutManager Name="dockLayoutManager1" ItemsSource="{Binding WidgetList}" FloatingMode="Desktop"
dx:WorkspaceManager.IsEnabled="True"
dxd:RestoreLayoutOptions.RemoveOldPanels="True"
dxd:RestoreLayoutOptions.AddNewPanels="True"
BeforeItemAdded="dockLayoutManager1_BeforeItemAdded" >
<dxd:LayoutGroup x:Name="group">
<dxd:LayoutPanel Visibility="Collapsed" />
<dxd:DocumentGroup x:Name="PanelHost" DestroyOnClosingChildren="False">
</dxd:DocumentGroup>
</dxd:LayoutGroup>
<!--<dxd:DockLayoutManager.ContextMenuCustomizations>
<dxb:AddBarItemLinkAction>
<dxb:BarItemLinkSeparator />
</dxb:AddBarItemLinkAction>
<dxb:BarButtonItem Name="bAbout" CategoryName="Help" Content="About" />
</dxd:DockLayoutManager.ContextMenuCustomizations>
<dxd:DockLayoutManager.ItemSelectorMenuCustomizations>
<dxb:AddBarItemLinkAction>
<dxb:BarItemLinkSeparator />
</dxb:AddBarItemLinkAction>
<dxb:BarButtonItem Name="bHome" CategoryName="Help" Content="AAA" />
</dxd:DockLayoutManager.ItemSelectorMenuCustomizations>-->
</dxd:DockLayoutManager>
</DockPanel>
</dxb:BarManager>
</Window>
Image cannot resize as its height and width is fixed. Remove Height and width of the image.
<Grid>
<Image HorizontalAlignment="Left" Margin="96,104,0,0" Source="{Binding Image}" VerticalAlignment="Top" />
</Grid>
And also remove TextWrapping="Wrap" from image tag. TextWrapping is not a property of image control
try this code:
<Grid> <Image HorizontalAlignment="Left" Source="{Binding Image}" VerticalAlignment="Top" HorizontalAlignment="Left" /> </Grid>

Setting text property to a button doesn't work

This is my UserControl file:
<UserControl x:Class="myProject.ButtonWithImage"
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:myProject="clr-namespace:myProject;assembly=myProject.BusinessLogic"
mc:Ignorable="d"
Name="ImagedControl"
d:DesignHeight="300" d:DesignWidth="300">
<Button Height="35" Width="90" FocusVisualStyle="{x:Null}" Foreground="White"
Click="OnClick" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="{Binding ElementName=ImagedButton, Path=ImageSource}" Stretch="None" Margin="0 0 5 0" />
<TextBlock Text="{Binding ElementName=ImagedButton, Path=Text}" FontSize="12" VerticalAlignment="Center" />
</StackPanel>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" CornerRadius="8">
<Border.Background>
SlateBlue
</Border.Background>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
And in a page I set up a button like this:
<myProject:ButtonWithImage ImageSource="/Resources/test.png" Text="Back" Name="btnBack1" Command="NavigationCommands.GoToPage" CommandParameter="ViewModel/Categories.xaml" />
However the text and the imagesource aren't on the button! They don't appear. What am I doing wrong?
Created Button Usercontrol
<Button x:Class="WpfApplication5.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
FocusVisualStyle="{x:Null}" Content="ok" Foreground="White" Height="30" Width="90" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" CornerRadius="8">
<Border.Background>
SlateBlue
</Border.Background>
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="30" Width="30" Stretch="Fill" Margin="0 0 0 0" />
<TextBlock Text="{Binding Path=Content,RelativeSource={RelativeSource TemplatedParent}}" FontSize="12" VerticalAlignment="Center" />
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
c# code
public partial class UserControl1 : Button
{
public UserControl1()
{
InitializeComponent();
}
}
xaml window
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myProject="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<myProject:UserControl1 Content="Button Text" Tag="btn2.png" />
You have named your UserControl ImageControl, but then you have tried to access it in your XAML using the name ImageButton. You'll have more luck if you stick with the one name. Try this instead:
<UserControl x:Class="myProject.ButtonWithImage"
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:myProject="clr-namespace:myProject;assembly=myProject.BusinessLogic"
mc:Ignorable="d"
Name="ImagedControl"
d:DesignHeight="300" d:DesignWidth="300">
<Button Height="35" Width="90" FocusVisualStyle="{x:Null}" Foreground="White"
Click="OnClick" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="{Binding ElementName=ImagedControl, Path=ImageSource}" Stretch="None" Margin="0 0 5 0" />
<TextBlock Text="{Binding ElementName=ImagedControl, Path=Text}" FontSize="12" VerticalAlignment="Center" />
</StackPanel>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" CornerRadius="8">
<Border.Background>
SlateBlue
</Border.Background>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
This also assumes that you have correctly declared two DependencyPropertys of the correct type, named ImagesSource and Text in your UserControl and have set appropriate values for them.

How to set TemplateBinding to resolve WPF error "Cannot set a TemplateBinding if not in a template."?

I'm trying to implement the example outlined here:
http://www.codeproject.com/Articles/30994/Introduction-to-WPF-Templates
The author states "The ContentPresenter control can be used to display the content of a WPF control."
with the following code:
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"
Content="{TemplateBinding Button.Content}" />
I've added it to my window as follows:
<Window x:Class="HKC.Desktop.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="487" Width="765.924" Loaded="Window_Loaded">
<Grid x:Name="mainGrid" Background="#FF252525">
<Button Content="Push Me" Template="{StaticResource buttonTemplate}" Name="button1" Height="100" Width="100"></Button>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" />
</Grid>
</Window>
But I'm getting the following error:
Cannot set a TemplateBinding if not in a template.
How can I resolve this?
You need to put the ContentPresent in the ControlTemplate, like that
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
<Grid>
<Ellipse Name="el1" Fill="Orange" Width="100" Height="100">
</Ellipse>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"
Content="{TemplateBinding Button.Content}" />
</Grid>
</ControlTemplate>
The problem is that you have no template. Your XAML should look somehow like this:
<Window x:Class="HKC.Desktop.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="487" Width="765.924" Loaded="Window_Loaded">
<Window.Resources>
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
<Ellipse Name="el1" Fill="Orange" Width="100" Height="100">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" />
</ControlTemplate>
</Window.Resources>
<Grid x:Name="mainGrid" Background="#FF252525">
<Button Content="Push Me" Template="{StaticResource buttonTemplate}" Name="button1" Height="100" Width="100"/>
</Grid>
</Window>

WPF control positioning problem

i am learning WPF here is my XAML.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<StackPanel>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</StackPanel>
button textbox is showing but the problem is I am not being able to drag the control to another location. how to fix it. please help. thanks
If you mean you can't drag the Button controls to different locations, it's because they're contained within a StackPanel - stacking them one on top of each other.
If you change that StackPanel to be a Grid, you'd have the ability to drag it around in a canvas-like manner.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<Grid>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</Grid>
This question may shed some light on where to use Grids and StackPanels.
If by "able to drag the control to another location" you are talking about repositioning the control using Expression Blend or the Visual Studio Designer you need to change the StackPanel to a Grid
So it would become-
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<Grid>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</Grid>

Resources