How to move a Image using XAML? - wpf

I want to move a Image control in WPF (Actually, the Image will move from left to right then to left then to right, looply repeat this.), I want to use XAML to control it. Also, I want the image control to move when hiting MouseEnter event.
Below is my Image in a Grid:
<Window
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" x:Class="TimeZoneDaemonApp.Window1"
Title="TimeZone Browser (CodeBy:CS48516)" Icon="/TimeZoneDaemonApp;component/Images/Settings.png" Background="{x:Null}" WindowStartupLocation="Manual" Width="704" Height="170" Opacity="1" WindowStyle="None" AllowsTransparency="True">
<Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown" PreviewMouseRightButtonDown="Grid_PreviewMouseRightButtonDown" >
<Grid.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</Grid.Resources>
<Border OpacityMask="White" Height="100" >
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset=".6"/>
<GradientStop Color="Green" Offset=".9"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.Column="0"><TextBlock FontFamily="#微软雅黑" Padding="0,5,0,0" FontSize="12pt" Foreground="Black" FontWeight="bold" Background="#FFF87800" TextAlignment="Center"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="1"><TextBlock Background="#FFB0D428" Padding="0,5,0,0" FontFamily="#微软雅黑" FontSize="16" FontWeight="Bold" Foreground="Black" Text="New York" TextAlignment="Center"/></Border>
<Border Grid.Column="2"><TextBlock Background="#FF2283E4" Padding="0,5,0,0" FontFamily="#微软雅黑" FontSize="16" FontWeight="Bold" Foreground="Black" Text="London" TextAlignment="Center"/></Border>
<Border Grid.Column="3"><TextBlock Background="#FF20D4D0" Padding="0,5,0,0" FontFamily="#微软雅黑" FontSize="16" FontWeight="Bold" Foreground="Black" Text="Pairs" TextAlignment="Center"/></Border>
<Border Grid.Column="4"><TextBlock Background="#FF28D428" Padding="0,5,0,0" FontFamily="#微软雅黑" FontSize="16" FontWeight="Bold" Foreground="Black" Text="Sydney" TextAlignment="Center"/></Border>
<Border Grid.Column="5"><TextBlock Background="#FFD040F8" Padding="0,5,0,0" FontFamily="#微软雅黑" FontSize="16" FontWeight="Bold" Foreground="Black" Text="Brasilia" TextAlignment="Center"/></Border>
<Border Grid.Column="0" Grid.Row="1"><TextBlock x:Name="HK" Padding="0,5,0,0" Background="#FFA04C00" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="1" Grid.Row="1"><TextBlock x:Name="NY" Padding="0,5,0,0" Background="#FF708418" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="2" Grid.Row="1"><TextBlock x:Name="UK" Padding="0,5,0,0" Background="#FF184880" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="3" Grid.Row="1"><TextBlock x:Name="PS" Padding="0,5,0,0" Background="#FF188480" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="4" Grid.Row="1"><TextBlock x:Name="SD" Padding="0,5,0,0" Background="#FF188418" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="5" Grid.Row="1"><TextBlock x:Name="BR" Padding="0,5,0,0" Background="#FF7800A8" FontFamily="#微软雅黑" FontSize="12" Foreground="White" TextAlignment="Center" TextWrapping="NoWrap" TextTrimming="None"><Run Text="Hong Kong"/></TextBlock></Border>
<Border Grid.Column="0" Grid.Row="2"><TextBlock x:Name="HKT" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" FontWeight="Bold"><Run Text="Load"/></TextBlock></Border>
<Border Grid.Column="1" Grid.Row="2"><TextBlock x:Name="NYT" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" FontWeight="Bold"><Run Text="Load"/></TextBlock></Border>
<Border Grid.Column="2" Grid.Row="2"><TextBlock x:Name="UKT" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" FontWeight="Bold"><Run Text="Load"/></TextBlock></Border>
<Border Grid.Column="3" Grid.Row="2"><TextBlock x:Name="PST" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" FontWeight="Bold"><Run Text="Load"/></TextBlock></Border>
<Border Grid.Column="4" Grid.Row="2"><TextBlock x:Name="SDT" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" FontWeight="Bold"><Run Text="Load"/></TextBlock></Border>
<Border Grid.Column="5" Grid.Row="2"><TextBlock x:Name="BRT" Padding="0,5,0,0" FontFamily="#Gill Sans MT" FontSize="21pt" Foreground="White" TextAlignment="Center" TextDecorations="None" FontWeight="Bold" Background="Transparent"><Run Text="Load"/></TextBlock></Border>
</Grid>
</Border>
<Image HorizontalAlignment="Left" Height="50" Margin="0,0,0,0" Source="Images/cloud.png" Stretch="Fill" VerticalAlignment="Top" Width="190">
</Image>
</Grid>
I am a new learner for WPF, AnyOne can Help on this? thx.
Actually I have found a chapter in SOF: WPF. Easiest way to move Image to (X,Y) programmatically?
Well, that's not what I want.

My Answer listed below:
we need a Canvas to contain the images and we can use the "Left" property of the Canvas to move the Image.
<Canvas x:Name="MyCanvas">
<Canvas.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard >
<Storyboard Storyboard.TargetName="img1" Storyboard.TargetProperty="(Canvas.Left)">
<DoubleAnimation From="0" To="514" Duration="0:0:20" AccelerationRatio=".2" DecelerationRatio=".8" RepeatBehavior="23:59:59" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>

Related

WPF- Add Item in ItemsControl Will Not Change Border Cornerradius

I have a Border and I Change it's Cornerradius to 19.
Meanwhile, I have a ScrollViewer, it contains a ItemsControl includeing a Label, a TextBox, and a Border.
I find content in ItemsControl will cover original Border. So Border's Cornerradius is not 19.
Image below as:
It's my xaml:
<Border Grid.Row="3" Background="#d8d8d8" CornerRadius="19" Margin="24,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="46"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Foreground="#011627" FontFamily="Arial" FontSize="18" Content="Feedback" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Label Grid.Row="0" Grid.Column="1" Foreground="#011627" FontFamily="Arial" FontSize="18" Content="Feedback codes" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding SystemStatusList}" AlternationCount="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent" x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" BorderThickness="0" Foreground="#d8d8d8" Height="42" FontFamily="Arial" FontSize="18" Content="{Binding SystemStatus}"
HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" Margin="1,0"/>
<TextBox Grid.Column="1" BorderThickness="0" Foreground="White" Height="42" Background="{x:Null}" FontFamily="Arial" FontSize="18" Text="{Binding CustomName}" CaretBrush="White"
HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" Margin="0,0,1,0"/>
<Border Grid.Column="1" Width="165" Height="1" BorderThickness="1" BorderBrush="#979797" Opacity="0.24" Margin="0,24,0,0"/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter TargetName="grid" Property="Background" Value="#384451"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter TargetName="grid" Property="Background" Value="#2c3845"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Border Grid.Row="0" Grid.Column="0" Width="2" Height="46" Opacity="0.24" BorderThickness="2" BorderBrush="#979797" HorizontalAlignment="Right"/>
<Border Grid.Row="1" Grid.Column="0" Width="2" Height="763" Opacity="0.24" BorderThickness="2" BorderBrush="#979797" HorizontalAlignment="Right"/>
</Grid>
</Border>
What can I do? Thanks!
Edit:
Use a custom Border implementation that supports clipping or specify a bottom margin for the ScrollViewer:
<ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Auto"
Margin="0 0 0 20">
<ItemsControl ItemsSource="{Binding SystemStatusList}" AlternationCount="2">
I find a answer as below.
I add border OpacityMask:
<Border.OpacityMask>
<DrawingBrush Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#d8d8d8">
<GeometryDrawing.Geometry>
<RectangleGeometry RadiusX="19" RadiusY="19" Rect="0,0,593,741"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.OpacityMask>
And use second Border to cover first Border:
<Border Grid.Row="3" BorderThickness="1" BorderBrush="#d8d8d8" CornerRadius="19" Margin="24,0"/>
It finally show as:

Xaml intellisense not working

I have a problem with the intellisense. It works in code behind but not in xaml code (wpf).
1st image: Intelissense not working
2nd image: I comment on the 1st usercontrol and the intellisense works.
Here the code is in the usercontrol "cuAccessoiresAutres":
<UserControl x:Class="MicroSecu.ControleUtilisateur.cuAccessoiresAutres"
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:ControleUtilisateur="clr-namespace:MicroSecu.ControleUtilisateur"
mc:Ignorable="d"
d:DesignHeight="251" d:DesignWidth="672">
<UserControl.Resources>
<SolidColorBrush x:Key="controlBorderBrush" Color="LightGray" />
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<!--En tête-->
<Border Grid.Row="0" Grid.Column="0" BorderThickness="0,0,1,1" BorderBrush="{StaticResource controlBorderBrush}"></Border>
<Border Grid.Row="0" Grid.Column="1" BorderThickness="1,2,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Gauche" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"></TextBlock>
</Border>
<Border Grid.Row="0" Grid.Column="2" BorderThickness="1,2,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Droit" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"></TextBlock>
</Border>
<!--Profil porteur-->
<Border Grid.Row="1" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Profil porteur" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="12" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="1" Grid.Column="1" BorderThickness="1,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.ProfilPorteurGauche}" IsEnabled="{Binding MaConsole.ProfilPorteurGaucheEstDispo}"></CheckBox>
</Border>
<Border Grid.Row="1" Grid.Column="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.ProfilPorteurDroit}" IsEnabled="{Binding MaConsole.ProfilPorteurDroitEstDispo}"></CheckBox>
</Border>
<!--Anneau décalé-->
<Border Grid.Row="2" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Anneau décalé" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="12" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="2" Grid.Column="1" BorderThickness="1,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.AnneauDécaléGauche}" IsEnabled="{Binding MaConsole.AnneauDécaléGaucheEstDispo}"></CheckBox>
</Border>
<Border Grid.Row="2" Grid.Column="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.AnneauDécaléDroit}" IsEnabled="{Binding MaConsole.AnneauDécaléDroitEstDispo}"></CheckBox>
</Border>
<!--Glissière de reprise-->
<Border Grid.Row="3" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Glissière de reprise" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.GlissièreDeReprise}" IsEnabled="{Binding MaConsole.GlissièreDeRepriseEstDispo}"></CheckBox>
</Border>
<!--Glissière de reprise plateau arrière-->
<Border Grid.Row="4" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Glissière de reprise plateau arrière" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.GlissièreDeReprisePlateauArrière}" IsEnabled="{Binding MaConsole.GlissièreDeReprisePlateauArrièreEstDispo}"></CheckBox>
</Border>
<!--Tôle sur extensible-->
<Border Grid.Row="5" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Tôle sur extensible" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.TôleSurExtensible}" IsEnabled="{Binding MaConsole.TôleSurExtensibleEstDispo}"></CheckBox>
</Border>
<!--Plaque verte RMTCE-->
<Border Grid.Row="6" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Plaque verte RMTCE" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1,1,2,1" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.PlaqueVerteRMTCE}" IsEnabled="{Binding MaConsole.PlaqueVerteRMTCEEstDispo}"></CheckBox>
</Border>
<!--Plaque jaune benne 1000L-->
<Border Grid.Row="7" Grid.Column="0" BorderThickness="2,1,1,1" BorderBrush="{StaticResource controlBorderBrush}">
<TextBlock Text="Plaque jaune benne 1000L" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap"></TextBlock>
</Border>
<Border Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1,1,2,2" BorderBrush="{StaticResource controlBorderBrush}">
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding MaConsole.PlaqueJaune1000L}" IsEnabled="{Binding MaConsole.PlaqueJaune1000LEstDispo}"></CheckBox>
</Border>
</Grid>

Solve Set property 'System.Windows.Controls.Decorator.Child' threw an exception in DataTemplate

I have created the following datatemplate:
<DataTemplate x:Key="StudentDataTemplate" DataType="local:IMDataSet+StudentRow" >
<DataTemplate.Resources>
<local:PhoneNumberConverter x:Key="phoneNumConv"/>
<local:SSIDConverter x:Key="SSIDNum"/>
<local:GenderConverter x:Key="genderConverter"/>
<local:DateToAgeConverter x:Key="dateToAgeConverter"/>
<local:ShamsiConverter x:Key="shamsiConverter"/>
</DataTemplate.Resources>
<Grid Name="grdContainer" HorizontalAlignment="Right" MaxWidth="800" MinWidth="250" >
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition Height="70"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Drop Shadow-->
<Border x:Name="BrdShadow" BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="2">
<Border.Effect>
<DropShadowEffect BlurRadius="18" Direction="0" ShadowDepth="0"/>
</Border.Effect>
</Border>
<Border x:Name="BrdFrame" BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="3">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="WhiteSmoke"/>
<GradientStop Color="#FFF8F8F8" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border Margin="10" x:Name="BrdImageFrame" Child="{StaticResource StudentPic}" BorderThickness="0.5" BorderBrush="white" CornerRadius="0,0,2,2" Grid.RowSpan="2" Background="White">
<Border.Effect>
<DropShadowEffect BlurRadius="5" Direction="0" ShadowDepth="0"/>
</Border.Effect>
</Border>
<StackPanel Margin="1" Grid.Column="1" Grid.RowSpan="2">
<StackPanel Orientation="Horizontal" Margin="0,0,5,0">
<TextBlock Name="BlkGender" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=Gender, Converter={StaticResource genderConverter}}" VerticalAlignment="Center" Margin="0,0,7,-5" />
<TextBlock x:Name="BlkFullName" TextWrapping="Wrap" FontSize="26.667" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HighImportanceText}" VerticalAlignment="Center" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Name"/>
<Binding Path="Family"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="blkAge" Text="{Binding Path=BornDate, Converter={StaticResource dateToAgeConverter }, StringFormat={} - {0} ساله}" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" ToolTip="{Binding Path=BornDate, Converter={StaticResource shamsiConverter}}" VerticalAlignment="Center" TextAlignment="Center" Margin="0,9,0,0" />
</StackPanel>
<TextBlock Name="blkFather" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}" Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra" Text="{Binding Path=FatherName, StringFormat=فرزند: {0}}" />
<TextBlock Name="blkDefContact" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}" Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra" Text="{Binding Path=DefaultContact, Converter={StaticResource phoneNumConv}}" FlowDirection="LeftToRight" HorizontalAlignment="Left" />
</StackPanel>
<Expander x:Name="expander" IsExpanded="True" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.Row="2" Grid.ColumnSpan="2" Style="{DynamicResource ExpanderStyle1}" ExpandDirection="Down" >
<StackPanel Margin="10,10,10,15" x:Name="stkFinName">
<TextBlock x:Name="FinName" TextWrapping="Wrap" FontSize="20" FontFamily="Calibri" FontWeight="Bold" Foreground="{DynamicResource HalfImportanceText}" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="EnglishName"/>
<Binding Path="EnglishFamily"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<StackPanel Orientation="Horizontal" Margin="8,5,5,0">
<TextBlock x:Name="blkBornLoc" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=BornLocation, StringFormat=تولد: {0}}" Margin="0,0,5,0" />
<TextBlock x:Name="blkSSID" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=SSID, Converter={StaticResource SSIDNum}}" Margin="5,0,0,0" FlowDirection="LeftToRight" HorizontalAlignment="Left" />
</StackPanel>
<TextBlock x:Name="blkLicense" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding License}" Margin="10,0,10,0" />
<TextBlock x:Name="blkAddress" TextWrapping="Wrap" MaxWidth="250" HorizontalAlignment="Left" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Address}" Margin="10,5,10,0" />
</StackPanel>
</Expander>
</Grid>
</DataTemplate>
When i create one instace from it, there is no problem but when i want to create multiple instances (like using it in a listBox as itemTemplate) it throws the following Exception
Set property 'System.Windows.Controls.Decorator.Child' threw an exception.
InnerException: Specified element is already the logical child of another element. Disconnect it first.
The problem is caused by the following line:
<Border ... Child="{StaticResource StudentPic}" ...>
You can't reuse controls like this. You should put the control into the template instead.

Styling a GroupBox

I'm trying to create a GroupBox design like this.
I have looked at the GroupBox.HeaderTemplate
but I'm having problems creating the blue background color, with a width of 100%.
The same goes for the border.
My code so far
<GroupBox.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding}" HorizontalAlignment="Stretch" Background="#25A0DA" Grid.Column="0" Height="20" Padding="5,0,0,0" Margin="1" Foreground="White"/>
</Grid>
</DataTemplate>
</GroupBox.HeaderTemplate>
And this is what it looks like right now.
Take the default GroupBox Template and alter it to look the way you want
For example,
<ControlTemplate TargetType="GroupBox">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Grid.Row="0"
BorderThickness="1"
BorderBrush="#25A0DA"
Background="#25A0DA">
<Label Foreground="White">
<ContentPresenter Margin="4"
ContentSource="Header"
RecognizesAccessKey="True" />
</Label>
</Border>
<Border Grid.Row="1"
BorderThickness="1,0,1,1"
BorderBrush="#25A0DA">
<ContentPresenter Margin="4" />
</Border>
</Grid>
</ControlTemplate>
This thread is a bit old, but someone could find this useful...
A small modification to Jakob's answer if you want to have full width header.
You can bind to the parent GroupBox, so you can use it without having a named GroupBox.
<GroupBox.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupBox}, Path=ActualWidth }"
Background="#25A0DA" Grid.Column="0" Height="20" Padding="5,0,0,0" Margin="1" Foreground="White"/>
</DataTemplate>
</GroupBox.HeaderTemplate>
You probably will not be able to make it look exactly like your example without writing a completely different template but I gave it a simple shot by binding the width of the grid in your HeaderTemplate to the width of the groupbox and by specifying appropriate margin and padding:
<GroupBox.HeaderTemplate>
<DataTemplate>
<Grid Width="{Binding ElementName=groupBox1, Path=ActualWidth}" Margin="-10, 0, -10, 0" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Content="{Binding}" HorizontalAlignment="Stretch" Background="#25A0DA" Grid.Column="0" Height="20" Padding="5, 0, 0, 0" Margin="10" Foreground="White"/>
</Grid>
</DataTemplate>
</GroupBox.HeaderTemplate>
The result looks like this:
I realize this is way late, but the MahApps.Metro package has a nice GroupBox that seems to like nearly exactly like what is posted in the OP.
https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Styles/Controls.GroupBox.xaml
Here's the Xaml from version 1.22
<ResourceDictionary 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"
xmlns:Converters="clr-namespace:MahApps.Metro.Converters">
<Converters:ThicknessBindingConverter x:Key="ThicknessBindingConverter" />
<Style x:Key="MetroGroupBox" TargetType="{x:Type GroupBox}">
<Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
<Setter Property="Background" Value="{DynamicResource AccentColorBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="{DynamicResource ContentFontSize}" />
<Setter Property="Controls:GroupBoxHelper.HeaderForeground" Value="{x:Null}" />
<Setter Property="Margin" Value="5" />
<Setter Property="Padding" Value="5" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupBox}">
<Grid x:Name="GroupBoxRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border x:Name="HeaderSite"
Grid.Row="0"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="True">
<Controls:ContentControlEx x:Name="HeaderContent"
Padding="{TemplateBinding Padding}"
FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
Content="{TemplateBinding Header}"
ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
ContentStringFormat="{TemplateBinding HeaderStringFormat}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="False">
<TextElement.Foreground>
<MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Background"
Mode="OneWay" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="(Controls:GroupBoxHelper.HeaderForeground)"
Mode="OneWay" />
</MultiBinding>
</TextElement.Foreground>
</Controls:ContentControlEx>
</Border>
<Border Grid.Row="1"
Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Top}}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="True">
<ContentPresenter Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
UseLayoutRounding="False" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Try this :
<GroupBox BorderThickness="0" Header="BELT WEIGHER BC#1 JETTY" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="193" Width="374" OpacityMask="Black" BorderBrush="#FF1864D3" FontSize="16" FontWeight="Bold">
<GroupBox.HeaderTemplate >
<DataTemplate>
<TextBlock Text="{Binding}"
Width="357" Grid.Column="0" Padding="5,3,0,0" Margin="0,0,0,0" Foreground="White" Height="33">
<TextBlock.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0.968"/>
<GradientStop Color="Blue" Offset="0.828"/>
</LinearGradientBrush>
</TextBlock.Background>
</TextBlock>
</DataTemplate>
</GroupBox.HeaderTemplate>
<Border x:Name="CanvasBorder" BorderBrush="Blue" BorderThickness="1" Margin="3,0,3,0">
<Canvas Background="white" Margin="0,0,0,0" >
<Label Content="Feed Rate" Canvas.Left="10" Canvas.Top="10" FontWeight="Normal" FontSize="12"/>
<Label Content="Totalizer 1" Canvas.Left="10" Canvas.Top="35" FontWeight="Normal" FontSize="12"/>
<Label Content="Totalizer 2" Canvas.Left="10" Canvas.Top="60" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
<Label Content="Belt Load" Canvas.Left="10" Canvas.Top="85" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
<Label Content="Belt Speed" Canvas.Left="10" Canvas.Top="110" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
<TextBlock x:Name="BC1_Feedrate" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="13" Padding="0,0,4,0"/>
<TextBlock x:Name="BC1_Totalizer1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="38" Padding="0,0,4,0"/>
<TextBlock x:Name="BC1_Totalizer2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="63" Padding="0,0,4,0"/>
<TextBlock x:Name="BC1_BeltLoad" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="88" Padding="0,0,4,0"/>
<TextBlock x:Name="BC1_BeltSpeed" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="113" Padding="0,0,4,0"/>
<Label Content="ton/hour" Canvas.Left="228" Canvas.Top="10" FontWeight="Normal" FontSize="12"/>
<Label Content="ton" Canvas.Left="228" Canvas.Top="35" FontWeight="Normal" FontSize="12"/>
<Label Content="ton" Canvas.Left="228" Canvas.Top="60" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
<Label Content="kg/meter" Canvas.Left="228" Canvas.Top="85" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
<Label Content="meter/second" Canvas.Left="228" Canvas.Top="110" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
</Canvas>
</Border>
</GroupBox>

WPF Image stretch to fill doesn't stretch to the bounds of it's parents

Here's my XAML and how it looks like:
<Window x:Class="GameLenseWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="350" MinHeight="450" MinWidth="350">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.15*" />
<RowDefinition />
</Grid.RowDefinitions>
<Image Grid.Row="0" Stretch="Fill" Source="Image/topBarBg.png" />
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock Text="Platform"
Foreground="White"
FontFamily="Georgia"
FontSize="15"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<ComboBox x:Name="cmbPlatform"
Margin="10"
FontFamily="Georgia"
FontSize="15"
MinHeight="30"
MinWidth="140"
VerticalAlignment="Center"
VerticalContentAlignment="Center" SelectionChanged="cmbPlatform_SelectionChanged">
<ComboBoxItem>All Platforms</ComboBoxItem>
<ComboBoxItem>Playstation 3</ComboBoxItem>
<ComboBoxItem>XBox 360</ComboBoxItem>
<ComboBoxItem>Wii</ComboBoxItem>
<ComboBoxItem>PSP</ComboBoxItem>
<ComboBoxItem>DS</ComboBoxItem>
</ComboBox>
</StackPanel>
<Image x:Name="imgAbout" Grid.Row="0" Source="Image/about.png"
Height="16" HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="0 0 10 0" />
<ListBox Grid.Row="1" x:Name="lstGames" Background="#343434" Padding="5"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<!-- SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- SelectedItem without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- SelectedItem text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="120" Margin="0 10" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Border BorderBrush="#202020" BorderThickness="5" CornerRadius="4" Panel.ZIndex="0">
<Canvas Grid.Row="0" Grid.Column="0" >
<Image Source="{Binding ImageUrl}" Canvas.Left="0" Canvas.Top="0" Stretch="UniformToFill"/>
<Image Source="Image/youtube.png" Canvas.Bottom="0" Canvas.Right="0" Height="20" Width="20" />
</Canvas>
</Border>
<StackPanel Grid.Row="0" Grid.Column="1" Margin="12 0 0 0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title:" FontFamily="Arial" Foreground="White"/>
<TextBlock Text="{Binding Title}" FontFamily="Arial" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Release Date:" FontFamily="Arial" Foreground="White" />
<TextBlock Text="{Binding ReleaseDate}" FontFamily="Arial" Foreground="White" />
</StackPanel>
<TextBlock Text="Synopsis" FontFamily="Arial" Foreground="White" />
<TextBox Background="#454545" Foreground="White" TextWrapping="Wrap" Text="{Binding Synopsis}" MaxHeight="73" />
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
I just want to games image to stretch to fill the canvas bounds, which is inside of the rounder border.
Why is it not doing this?
The problem is that you have the images within a canvas. Basically, any time you put an element in a Canvas panel, you are disregarding the entire layout system. Elements within a canvas do not participate in layout at all. What you want to do is replace the Canvas element with a plain Grid element (you don't need to define rows or columns, it will default to a single row/column.)
EDIT
And actually looking more closely at what you are trying to do, you'll need to make some slight tweaks. Try this.
<Border BorderBrush="#202020" BorderThickness="5" CornerRadius="4">
<Grid>
<Image Source="{Binding ImageUrl}" Stretch="UniformToFill"/>
<Image Source="Image/youtube.png" HorizontalAlignment="Right" VerticalAlignment="Bottom" Height="20" Width="20" />
</Grid>
</Border>
your problem is here
<Border BorderBrush="#202020" BorderThickness="5" CornerRadius="4" Panel.ZIndex="0">
<Canvas Grid.Row="0" Grid.Column="0" >
<Image Source="{Binding ImageUrl}" Canvas.Left="0" Canvas.Top="0" Width="80" Stretch="Fill"/>
<Image Source="Image/youtube.png" Canvas.Bottom="0" Canvas.Right="0" Height="20" Width="20" />
</Canvas>
</Border>
you have a BorderThickness Attribute which puts the black border around the image, just remove it or set it to 0 and this will solve the problem.
so your code will be
<Border BorderBrush="#202020" CornerRadius="4" Panel.ZIndex="0">
<Canvas Grid.Row="0" Grid.Column="0" >
<Image Source="{Binding ImageUrl}" Canvas.Left="0" Canvas.Top="0" Width="80" Stretch="Fill"/>
<Image Source="Image/youtube.png" Canvas.Bottom="0" Canvas.Right="0" Height="20" Width="20" />
</Canvas>
</Border>
or
<Border BorderBrush="#202020" BorderThickness="0" CornerRadius="4" Panel.ZIndex="0">
<Canvas Grid.Row="0" Grid.Column="0" >
<Image Source="{Binding ImageUrl}" Canvas.Left="0" Canvas.Top="0" Width="80" Stretch="Fill"/>
<Image Source="Image/youtube.png" Canvas.Bottom="0" Canvas.Right="0" Height="20" Width="20" />
</Canvas>
</Border>

Resources