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>
Related
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:
I am trying to show the validation error message of data grid row in a tooltip. Here is the code that works when I use the ToolTip property of the Grid control directly (wihtout styling):
<Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
Now this shows the tooltip correctly. Everything is fine.
Problem:
As soon as I start styling the ToolTip separately, the binding stops working somehow. Here is the code that I am trying which does not work:
<Grid Margin="0,-2,0,-2">
<Ellipse x:Name="ErrorEllipse" StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center"/>
<Grid.ToolTip>
<ToolTip Background="Transparent" BorderBrush="Transparent" Height="Auto" Width="Auto">
<Border >
<TextBlock Text= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}"
MinHeight="20"
MinWidth="100"
Foreground="White"
Background="Red"/>
</Border>
</ToolTip>
</Grid.ToolTip>
</Grid>
What am I missing here and how can I achieve the proper binding? Probably its something basic but I have no idea...
You should style the ToolTip separately with a ControlTemplate.
<Style x:Key="ValidationToolTipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="Background" Value="Transparent" />
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Border >
<TextBlock
Text="{TemplateBinding Content}"
MinHeight="20"
MinWidth="100"
Foreground="White"
Background="Red"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then in your XAML, use the ToolTip like this:
<Grid Margin="0,-2,0,-2">
<Ellipse x:Name="ErrorEllipse"
StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock
Text="!"
FontSize="{TemplateBinding FontSize}"
FontWeight="Bold"
Foreground="White"
HorizontalAlignment="Center" />
<ToolTipService.ToolTip>
<ToolTip
Style="{StaticResource ValidationToolTipStyle}"
Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Path=(Validation.Errors)[0].ErrorContent}" />
</ToolTip>
</ToolTipService.ToolTip>
</Grid>
I hope this helps.
I m trying to create the expander with the header on right side and than add a grid to that exapnder. when i run this grids textblock doesnt apper. And like to know what are best way to create expander header on right side.
<StackPanel >
<Expander Header="Customer Detail" IsExpanded="False" Style="{StaticResource MainViewExpander}" >
<Expander.Content>
<StackPanel HorizontalAlignment="Stretch">
<Grid x:Name="ImageInformation" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.8*"/>
<ColumnDefinition Width="1.4*"/>
<ColumnDefinition Width="2.8*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.9*" />
<RowDefinition Height="0.7*" />
<RowDefinition Height="0.7*" />
</Grid.RowDefinitions>
<Image Source="{Binding XPath=x.null}" Grid.Column="0" Grid.RowSpan="3"/>
<TextBlock Name="Customername"
Grid.Column="1"
Grid.ColumnSpan="2"
Text="Customer Name"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
TextWrapping="NoWrap" Margin="0,0,0,13" Width="140" Height="22"/>
<TextBlock Name="Born"
Grid.Column="1"
Grid.Row="1"
Text="Born"
HorizontalAlignment="Left"
VerticalAlignment="Center"
TextWrapping="NoWrap" Margin="2" Width="60" />
<TextBlock Name="BornDetail"
Grid.Column="2"
Grid.Row="1"
Text="BornDetail"
HorizontalAlignment="Left"
VerticalAlignment="Center"
TextWrapping="NoWrap" Margin="2" Width="125" />
<TextBlock Name="Speak"
Grid.Column="1"
Grid.Row="2"
Text="Speak"
HorizontalAlignment="Left"
VerticalAlignment="Center"
TextWrapping="NoWrap" Margin="2" Width="60" />
<TextBlock Name="SpeakDetail"
Grid.Column="2"
Grid.Row="2"
Text="Speak"
HorizontalAlignment="Left"
VerticalAlignment="Center"
TextWrapping="NoWrap" Margin="2" Width="125" Height="16" MaxHeight="20" MaxWidth="135"/>
</Grid>
<Grid Height="150" Width="295">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" ></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.2*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="" Grid.Column="0" Grid.Row="0" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="0" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="1" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="1" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="2" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="2" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="3" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="3" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="4" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="4" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="5" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="5" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="6" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="6" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="7" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="7" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="8" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="8" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="0" Grid.Row="9" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="" Grid.Column="1" Grid.Row="9" TextWrapping="NoWrap" HorizontalAlignment="Left"></TextBlock>
</Grid>
</StackPanel>
</Expander.Content>
</Expander>
</StackPanel>
My stylesheet is something like this:
<UserControl.Resources >
<!-- Main Expander Header Brushes -->
<SolidColorBrush x:Key="MainExpanderHeaderBackgroundBrush" Color="#FF3771C1"/>
<SolidColorBrush x:Key="MainExpanderHeaderBorderBrush" Color="Black"/>
<!-- Main Expander Content Brushes -->
<SolidColorBrush x:Key="MainExpanderContentBorderBrush" Color="Black" />
<!-- Main Expander Control Brushes (affect the whole control, not just header or content -->
<SolidColorBrush x:Key="MainExpanderControlNormalForegroundBrush" Color="White" />
<SolidColorBrush x:Key="MainExpanderControlDisabledForegroundBrush" Color="DarkGray" />
<SolidColorBrush x:Key="MainExpanderControlDisabledBackgroundBrush" Color="LightGray" />
<SolidColorBrush x:Key="MainExpanderControlDisabledBorderBrush" Color="LightGray" />
<!-- Main Expander Toggle Button Brushes -->
<SolidColorBrush x:Key="ExpanderToggleButtonBackgroundBrush" Color="White" />
<SolidColorBrush x:Key="ExpanderToggleButtonMouseOverFillBrush" Color="Green" />
<SolidColorBrush x:Key="ExpanderToggleButtonPressedFillBrush" Color="Yellow" />
<!-- Expander Toggle Button Template -->
<ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">
<!-- Note, the chevron toggle button gets its forground color from the templated parent.
The parent must set this to a valid color when it is disabled. -->
<Path
Name="Chevron"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 10 10 L 20 0 Z"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}"
/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Chevron" Property="Data" Value="M 0 10 L 10 0 L 20 10 Z" />
</Trigger>
<Trigger Property="ToggleButton.IsMouseOver" Value="true">
<Setter TargetName="Chevron" Property="Fill" Value="{StaticResource ExpanderToggleButtonMouseOverFillBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Chevron" Property="Fill" Value="{StaticResource ExpanderToggleButtonPressedFillBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="MainViewExpander" TargetType="Expander">
<Setter Property="Foreground" Value="{StaticResource MainExpanderControlNormalForegroundBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Name="ContentRow"/>
</Grid.RowDefinitions>
<Border
Name="HeaderBorder"
Grid.Row="0"
BorderThickness="1"
CornerRadius="0,0,0,0"
BorderBrush="{StaticResource MainExpanderHeaderBorderBrush}"
Background="{StaticResource MainExpanderHeaderBackgroundBrush}"
>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<ContentPresenter
Grid.Column="0"
Margin="4"
ContentSource="Header"
RecognizesAccessKey="True"
/>
<ToggleButton
Grid.Column="1"
IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
OverridesDefaultStyle="True"
Template="{StaticResource ExpanderToggleButton}"
Background="{StaticResource ExpanderToggleButtonBackgroundBrush}"
/>
</Grid>
</Border>
<Border
Name="ContentBorder"
Grid.Row="1"
BorderBrush="{StaticResource MainExpanderContentBorderBrush}"
BorderThickness="1,0,1,1"
CornerRadius="0,0,0,0"
>
<ContentPresenter Margin="4" />
</Border>
</Grid>
<!-- Triggers for the entire Expander Control -->
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content, Path=DesiredHeight}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource MainExpanderControlDisabledForegroundBrush}"/>
<Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource MainExpanderControlDisabledBackgroundBrush}" />
<Setter TargetName="HeaderBorder" Property="BorderBrush" Value="{StaticResource MainExpanderControlDisabledBorderBrush}" />
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{StaticResource MainExpanderControlDisabledBorderBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"
Foreground="{StaticResource MainExpanderControlNormalForegroundBrush}"
FontSize="18"
FontWeight="Bold"
/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MainViewExpanderCommands"
BasedOn="{StaticResource MainViewExpander}"
TargetType="Expander"
>
<Setter Property="FontSize" Value="20" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</UserControl.Resources >
so how to get the data of grid in expander?
There are three problems
The MainExpanderControlNormalForegroundBrush was White, so nothing was visible in the expander content area. Change it to black as shown below.
<SolidColorBrush x:Key="MainExpanderControlNormalForegroundBrush" Color="Black" />
In one of the triggers you are binding to an element with nameContent which is not there. You should be binding to ContentBorder as shown below.
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=ContentBorder, Path=DesiredHeight}" />
</Trigger>
Set your ContentRow Height to 0 to bring the expander functionality.
<RowDefinition Name="ContentRow" Height="0"/>
I have two separate styles in which I am trying to include the same basic elements. For example, HorizontalButton has this style:
<Style x:Key="HorizontalButton" TargetType="{x:Type custom:SampleButton}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type custom:DispatchButton}">
<Border Name="outerBorder" Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type custom:SampleGrid}}, Path=ActualWidth, Converter={StaticResource MathConverter}, ConverterParameter=x/7}">
<Border Name="innerBorder" BorderThickness="1" BorderBrush="WhiteSmoke" CornerRadius="1" Background="{TemplateBinding Background}">
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="4*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding Id}"></TextBlock>
<TextBlock Text="{TemplateBinding Code}" Margin="4,0,0,0"></TextBlock>
</StackPanel>
<TextBlock Text="{TemplateBinding Address}" TextWrapping="Wrap"></TextBlock>
</StackPanel>
<Rectangle Grid.Row="1" Height="1" Margin="2,0,2,0" Stroke="DarkGray" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Grid>
<Ellipse VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15" Fill="{TemplateBinding SampleColor}" />
<TextBlock Foreground="{TemplateBinding Background}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Sample}"></TextBlock>
</Grid>
<Image Width="16" Height="16" Source="{TemplateBinding SymbolImage}" Margin="2,0,0,0" />
</StackPanel>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" Name="content" />
</Grid>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And VerticalButton has this style:
<Style x:Key="VerticalButton" TargetType="{x:Type custom:SampleButton}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type custom:DispatchButton}">
<Border Name="outerBorder" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type custom:SampleGrid}}, Path=ActualHeight, Converter={StaticResource MathConverter}, ConverterParameter=x/7}">
<Border Name="innerBorder" BorderThickness="1" BorderBrush="WhiteSmoke" CornerRadius="1" Background="{TemplateBinding Background}">
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="4*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding Id}"></TextBlock>
<TextBlock Text="{TemplateBinding Code}" Margin="4,0,0,0"></TextBlock>
</StackPanel>
<TextBlock Text="{TemplateBinding Address}" TextWrapping="Wrap"></TextBlock>
</StackPanel>
<Rectangle Grid.Row="1" Height="1" Margin="2,0,2,0" Stroke="DarkGray" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Grid>
<Ellipse VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15" Fill="{TemplateBinding SampleColor}" />
<TextBlock Foreground="{TemplateBinding Background}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Sample}"></TextBlock>
</Grid>
<Image Width="16" Height="16" Source="{TemplateBinding SymbolImage}" Margin="2,0,0,0" />
</StackPanel>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" Name="content" />
</Grid>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
As you can see, outerBorder has different attributes set depending on whether or not the button is vertical or horizontal, but all of the inner elements from innerBorder inwards are identical. Is there a way I can do some kind of an include or reference in xaml so that I would only have to make changes to one instance of the inner elements to get the same results?
You might be able to use a ContentControl with the ContentTemplate set to a DataTemplate containing all your shared elements
<DataTemplate x:Key="MySharedXaml">
<!-- Shared XAML here -->
</DataTemplate>
then in your controls, simply use this wherever you want your shared XAML
<ContentControl ContentTemplate="{StaticResource MySharedXAML}">
<ContentPresenter />
</ContentControl>
The only thing I'm not sure of is the Bindings. You might need to tweak your XAML a bit to make sure the bindings get set correctly.
I have the following Style for a DocumentViewer:
<Style x:Key="MyDocumentViewerStyle" TargetType="{x:Type DocumentViewer}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DocumentViewer}">
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Focusable="False">
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="1" CornerRadius="2" HorizontalAlignment="Stretch" BorderThickness="0">
<ToolBar HorizontalAlignment="Center" Height="35"
ToolBarTray.IsLocked="True" KeyboardNavigation.TabNavigation="Continue" ToolBar.OverflowMode="Never">
<Button Command="ApplicationCommands.Print" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Print">-->
<Image Source="/ApniCureConsole;component/Images/Print.png" ToolTip="Print report" />
</Button>
<Separator />
<Button Command="NavigationCommands.IncreaseZoom" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Zoom In">-->
<Image Source="/ApniCureConsole;component/Images/MagnifyPlus.png" ToolTip="Zoom in" />
</Button>
<Button Command="NavigationCommands.DecreaseZoom" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Zoom Out" />-->
<Image Source="/ApniCureConsole;component/Images/MagnifyMinus.png" ToolTip="Zoom out" />
</Button>
<Separator />
<Button Margin="10,0,10,0"
Content="SAVE REPORT"
ToolTip="Save the report"
Style="{StaticResource CommandButton}"
Command="{Binding Path=SaveReportCommand}"
CommandParameter="{Binding ????}" <------WHAT DO I PUT HERE?
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</ToolBar>
</Border>
<ScrollViewer Grid.Row="0" CanContentScroll="true" HorizontalScrollBarVisibility="Auto" x:Name="PART_ContentHost" IsTabStop="true">
<ScrollViewer.Background>
<LinearGradientBrush EndPoint="0,0" StartPoint="0,1">
<GradientStop Color="#559CB7A4" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</ScrollViewer.Background>
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've defined the following elements in my application that use this Style:
<TabControl Style="{StaticResource MyTabControlStyle}" TabStripPlacement="Top" >
<TabItem Style="{StaticResource MyTabItemStyle}" IsTabStop="False">
<TabItem.Header>
<TextBlock Text="REPORT ONE" Height="18" Padding="10, 0" FontSize="14" Style="{StaticResource MyTabItemText}"/>
</TabItem.Header>
<DocumentViewer Name="ReportOne" IsTabStop="False" Document="{Binding Report1}" ContextMenu="{x:Null}" Style="{StaticResource MyDocumentViewerStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" BorderBrush="#FF00668E">
</DocumentViewer>
</TabItem>
<TabItem Style="{StaticResource MyTabItemStyle}" IsTabStop="False">
<TabItem.Header>
<TextBlock Text="REPORT TWO" Height="18" Padding="10, 0" FontSize="14" Style="{StaticResource MyTabItemText}"/>
</TabItem.Header>
<DocumentViewer Name="ReportTwo" IsTabStop="False" Document="{Binding Report2}" ContextMenu="{x:Null}" Style="{StaticResource MyDocumentViewerStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" BorderBrush="#FF00668E">
</DocumentViewer>
</TabItem>
</TabControl>
My question is, how can I find out what the value for the "Name" property is from the XAML, so I can pass it my ViewModel?
Assuming that you want to pass the Name of the DocumentViewer as the CommandParameter to the ViewModel, the easiest is to use
CommandParameter="{TemplateBinding Name}"
which will bind the name of the templated control to the CommandParameter.