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.
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 new to wpf. I need to have a window that list all accounts added. But my application shows a double line below the "Accounts". Not sure what caused this. could someHow to fix this? Here is my xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel x:Name="StackPanel1" Grid.Row="0" HorizontalAlignment="Left" Height="150" VerticalAlignment="Top" Width="444">
<TextBlock x:Name="AddAccountTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Height="60" Width="369" Text="Add account to allow for simple access to your company resources
" SnapsToDevicePixels="True"/>
<Button x:Name="AddAccount" Content="Add Account" HorizontalAlignment="Left" VerticalAlignment="Top" Width="140" RenderTransformOrigin="0.466,0.977" IsCancel="True" Height="40" Foreground="White" Background="Blue" Click="OnAddAccount"/>
</StackPanel>
<StackPanel x:Name="StackPanel2" Grid.Row="2" HorizontalAlignment="Left" Height="154" Width="444">
<TextBlock x:Name="AccountsTextBlock" TextWrapping="Wrap" Text="Accounts" Height="25" Width="68" FontSize="16" FontFamily="Tahoma"/>
<ListBox x:Name="accounts" Height="130" VerticalAlignment="Top">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="100"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border" BorderBrush="Gray" BorderThickness="0.5" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="LightSkyBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" Width="100" Source="{Binding Path=imagePath}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" Text="{Binding Path=userInfo}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
You may remove the borders of the ListBox by setting the BorderThickness property:
<ListBox x:Name="accounts" Height="130" VerticalAlignment="Top" Margin="100"
BorderThickness="0">
You may set it to 1,0,1,0 to remove only the top and bottom borders. The order is left, top, right, bottom.
I have a UWP application with a DataGrid defined in xaml.
The DataGrid contains grouped data. When it displays, there is a downward-facing carat to the left of the group headings that can be clicked, causing that group to collapse. As far as I can see, this is a default behavior that we didn't specifically ask for.
My customer doesn't like this and wants me to remove the functionality. How can I do this?
You can disable the functionality by modifying the DataGridRowGroupHeader.HeaderStyle. Simple way is to add IsHitTestVisible to False. This will disable the expander so the group wont get Collapsed.
<controls:DataGrid>
<controls:DataGrid.RowGroupHeaderStyles>
<Style TargetType="controls:DataGridRowGroupHeader">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</controls:DataGrid.RowGroupHeaderStyles>
</controls:DataGrid>
Edit
Although it doesn't eliminate the indicator.
Here's new style with removing indicator
<controls:DataGrid>
<controls:DataGrid.RowGroupHeaderStyles>
<Style TargetType="controls:DataGridRowGroupHeader">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="IsHitTestVisible" Value="False"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="MinHeight" Value="32"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:DataGridRowGroupHeader">
<Grid x:Name="RowGroupHeaderRoot" MinHeight="{TemplateBinding MinHeight}">
<Grid.Resources>
<ControlTemplate x:Key="ToggleButtonTemplate" TargetType="ToggleButton">
<Grid Background="{TemplateBinding Background}">
</Grid>
</ControlTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle x:Name="IndentSpacer" Grid.Column="1"/>
<ToggleButton x:Name="ExpanderButton" Grid.Column="2" Height="12" Width="12" Template="{StaticResource ToggleButtonTemplate}"
IsTabStop="False" Margin="12,0,0,0" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}"/>
<StackPanel Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Center" Margin="12,0,0,0">
<TextBlock x:Name="PropertyNameElement" Margin="4,0,0,0" Visibility="{TemplateBinding PropertyNameVisibility}" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
<TextBlock x:Name="PropertyValueElement" Margin="4,0,0,0" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
<TextBlock x:Name="ItemCountElement" Margin="4,0,0,0" Visibility="{TemplateBinding ItemCountVisibility}" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
</StackPanel>
<Rectangle x:Name="CurrencyVisual" Grid.ColumnSpan="5"
StrokeThickness="1" Fill="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False" Opacity="0"/>
<Grid x:Name="FocusVisual" Grid.ColumnSpan="5" IsHitTestVisible="False" Opacity="0">
<Rectangle StrokeThickness="2" Fill="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False"/>
<Rectangle StrokeThickness="1" Fill="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False" Margin="2"/>
</Grid>
<Rectangle x:Name="BottomGridLine" Grid.ColumnSpan="5" Height="1" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</controls:DataGrid.RowGroupHeaderStyles>
</controls:DataGrid>
I am Trying to Set the Control Template of the WPF colorpicker to rectangle. I want that only a rectangular shape is displayed in place of ColorPicker ComboBox. But When I place any control in ControlTemplate, I get the error that "Object reference not set to instance of an object".
This is my wpf code:
<wpfx:ColorPicker Name="ColorPicker1" Height="30" DisplayColorAndName="False"
Margin="29,72,366,209"
SelectedColorChanged="ColorPicker1_SelectedColorChanged">
<wpfx:ColorPicker.Template>
<ControlTemplate>
<Rectangle ></Rectangle>
</ControlTemplate>
</wpfx:ColorPicker.Template>
</wpfx:ColorPicker>
Any suggestions of what I am doing wrong?
The most probable reason is that ColorPicker control expects some element within its Template, usually such elements has special name which starts from "PART_...". I think in order to get more information about which element is missed you have to look into the default color picker template and find out which elements there and which names they have.
Probable fix scenario: find the name of the control which is mandatory for ColorPicker control and give this name to your rectangle.
Here is the ColorPicker default template:
<ControlTemplate TargetType="{x:Type local:ColorPicker}">
<Grid>
<ToggleButton x:Name="PART_ColorPickerToggleButton"
IsTabStop="True"
MinHeight="22"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
Style="{TemplateBinding ButtonStyle}">
<Grid Margin="2">
<Border x:Name="ColorOnly" Style="{StaticResource ColorDisplayStyle}" />
<Border x:Name="ColorAndName" Background="White" Visibility="Hidden">
<StackPanel Orientation="Horizontal">
<Border HorizontalAlignment="Left" Width="20" Margin="2,1,4,1" Style="{StaticResource ColorDisplayStyle}" BorderThickness="1" BorderBrush="#FFC9CACA" />
<TextBlock Text="{Binding SelectedColorText, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" />
</StackPanel>
</Border>
</Grid>
</ToggleButton>
<Popup x:Name="PART_ColorPickerPalettePopup" VerticalAlignment="Bottom" IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" StaysOpen="False" AllowsTransparency="True" Focusable="False" HorizontalOffset="1" VerticalOffset="1" PopupAnimation="Slide">
<Border BorderThickness="1" Background="{StaticResource PopupBackgroundBrush}" BorderBrush="{StaticResource ColorPickerDarkBorderBrush}" Padding="3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid x:Name="_gridStandardColorsHost" Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Available Colors -->
<Grid Grid.Row="1" Visibility="{TemplateBinding ShowAvailableColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding AvailableColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,0,0,1" />
<ListBox x:Name="PART_AvailableColors"
Grid.Row="1"
ItemsSource="{Binding AvailableColors, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ColorListStyle}" />
</Grid>
</Grid>
<!-- Standard Colors-->
<Grid Grid.Row="2" Visibility="{TemplateBinding ShowStandardColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding StandardColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,1,0,1" />
<ListBox x:Name="PART_StandardColors"
Grid.Row="1"
ItemsSource="{Binding StandardColors, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ColorListStyle}" />
</Grid>
</Grid>
<!-- Recent Colors-->
<Grid Grid.Row="3" Margin="0,1,0,1" Visibility="{TemplateBinding ShowRecentColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding RecentColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,1,0,1" />
<ListBox x:Name="PART_RecentColors"
Grid.Row="1"
ItemsSource="{Binding RecentColors, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ColorListStyle}" />
</Grid>
</Grid>
</Grid>
<!-- ColorCanvas -->
<Grid x:Name="_colorCanvasHost" Visibility="Collapsed">
<local:ColorCanvas x:Name="PART_ColorCanvas"
Background="Transparent"
BorderThickness="0"
SelectedColor="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
<Separator Grid.Row="1" HorizontalAlignment="Stretch" Margin="5,0,5,0" />
<!-- More Colors Button -->
<ToggleButton x:Name="_colorMode" Grid.Row="2" Content="Advanced" Margin="5" Visibility="{TemplateBinding ShowAdvancedButton, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Grid>
</Border>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="DisplayColorAndName" Value="True">
<Setter TargetName="ColorOnly" Property="Visibility" Value="Collapsed" />
<Setter TargetName="ColorAndName" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger SourceName="_colorMode" Property="IsChecked" Value="True">
<Setter TargetName="_colorMode" Property="Content" Value="Standard" />
<Setter TargetName="_colorCanvasHost" Property="Visibility" Value="Visible" />
<Setter TargetName="_gridStandardColorsHost" Property="Visibility" Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
I am having a form in maximize mode, within the form contains a HeaderContentControl.
Within the HeaderContentControl.Content, i added a DockLayout, but the problem is that DockLayout is not fit to the form height.
How can I resolve this problem? Here's the xaml file:
<Window x:Class="Prototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Prototype"
Title="XXX"
x:Name="frmMain"
Width="581" Height="340" ResizeMode="CanMinimize"
WindowStartupLocation="CenterScreen" WindowState="Maximized"
WindowStyle="None" IsHitTestVisible="True" Topmost="False" AllowsTransparency="True" Background="Transparent" Loaded="frmMain_Loaded">
<!-- Copyright Microsoft Corporation. All Rights Reserved. -->
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainWindow}">
<Border Background="#FF333333"
BorderBrush="#FFCCCCCC"
BorderThickness="1"
CornerRadius="5"
Padding='2'>
<HeaderedContentControl>
<HeaderedContentControl.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="19*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="212*" />
<ColumnDefinition Width="84*" />
<ColumnDefinition Width='Auto' />
</Grid.ColumnDefinitions>
<Rectangle Grid.ColumnSpan="3" Fill="#FF505050" />
<TextBlock FontSize="13"
FontWeight='Bold'
VerticalAlignment='Center'
Margin="6,5,3,6"
Text="XXX" Grid.ColumnSpan="2" OpacityMask="#FFCECECE" Foreground="#FFF3F3F3" Height="20" />
<Button x:Name='WindowCloseButton'
Grid.Column="2"
Width="17"
Height="17"
Cursor='Hand'
Margin="8,6,6,8"
VerticalAlignment='Center'
Click='WindowCloseButton_Click' FontFamily="Lucida Console">
<Button.Background>
<ImageBrush />
</Button.Background>
<Image Source="/Prototype;component/Resource/window-close.png"></Image>
</Button>
</Grid>
</HeaderedContentControl.Header>
<!-- New Content Area -->
<HeaderedContentControl.Content>
<ContentPresenter Content="{TemplateBinding Content}" />
</HeaderedContentControl.Content>
</HeaderedContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="#FF7B7B7B"></Setter>
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Foreground" Value="#333333"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#333333"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="23" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu Height="23" Name="menuContext" Margin="0,0" Background="#FF7B7B7B" Foreground="White" Grid.Row="0">
<MenuItem Header="File" Background="#FF7B7B7B" Foreground="White">
<MenuItem Header="Open" Margin="0,1"/>
<MenuItem Header="Save" Margin="0,1"/>
<MenuItem Header="Exit" Margin="0,1" UseLayoutRounding="True" />
</MenuItem>
</Menu>
<Grid Grid.Row="1" ShowGridLines="True">
<DockPanel LastChildFill="True">
<Border Height="25"
Background="SkyBlue"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25"
Background="SkyBlue"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25"
Background="LemonChiffon"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Bottom">
<TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
</Border>
<Border Width="200"
Background="PaleGreen"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Left">
<TextBlock Foreground="Black">Dock = "Left"</TextBlock>
</Border>
<Border Background="White"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Foreground="Black">This content will "Fill" the remaining space</TextBlock>
</Border>
</DockPanel>
</Grid>
</Grid>
</Window>
Problem here is HeaderedContentControl uses StackPanel internally to layout header and content.
To fix that, use a Grid instead or re-template HeaderedContentControl to use Grid.
Example:
<ControlTemplate TargetType="HeaderedContentControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Content="{TemplateBinding Header}" Grid.Row="0" />
<ContentControl Content="{TemplateBinding Content}" Grid.Row="1" />
</Grid>
</ControlTemplate>
Adding a similar answer to the accepted one, but which uses a DockPanel instead of a Grid for a simpler implementation with the same results.
Example with DockPanel:
<ControlTemplate TargetType="HeaderedContentControl">
<DockPanel>
<ContentControl Content="{TemplateBinding Header}" DockPanel.Dock="Top" />
<ContentControl Content="{TemplateBinding Content}" />
</DockPanel>
</ControlTemplate>