I would like to add a Click Handler to my ListViewItems. I have found many Tutorials on how to do that but I have problems recreating that inside my ResourceDictionary. My Question is how can I define the ItemContainerStyle there. It doesn't work like this:
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border BorderBrush="#5076A7" BorderThickness="1">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#FFFFFF" Offset="0.0"/>
<GradientStop Color="#C0D3EA" Offset="1.0"/>
</LinearGradientBrush>
</Border.Background>
<StackPanel TextElement.FontFamily="Segoe UI" TextElement.FontSize="12">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="15"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0"/>
<Button FontSize="7" Content="X" Grid.Column="1" Grid.Row="0"
Command="{Binding DataContext.DeleteButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding ItemId}"/>
</Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Padding="3,0,0,0" Text="{Binding Kunde}"/>
<TextBlock Padding="3,0,0,0" Text="|"/>
<TextBlock Padding="3,0,0,0" Text="{Binding IncidentId}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Padding="3,0,0,0" Text="{Binding Ort}"/>
<TextBlock Padding="3,0,0,0" Text="("/>
<TextBlock Text="{Binding Alternative}"/>
<TextBlock Text=")"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Betreff}" Value="Blocked">
<Setter Property="Template" Value="{StaticResource BlockedListViewItem}"/>
</DataTrigger>
</Style.Triggers>
</Style>
Move the Style to your MainWindow's Resources and add the EventSetter directly to the ListViewItem Style:
<Style TargetType="ListViewItem">
...
<EventSetter Event="PreviewMouseLeftButtonDown"
Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
</Style>
Related
I am not good at WPF and I am struggling with this a bit. I have a WPF window that has the following code:
<ContentControl.Content>
<Button Name="btnUpdateCommand" HorizontalAlignment="Right" Grid.Row="2" Command="{Binding UpdateCommand}" Height="23" Margin="0,4">
<StackPanel Orientation="Horizontal" Name="txtUpdate" HorizontalAlignment="Right">
<TextBlock Text="{Binding InstallButtonText}" DockPanel.Dock="Right" Margin="5,2,0,0" HorizontalAlignment="Right"></TextBlock>
<TextBlock Text="{Binding FormCloseCountDown}" DockPanel.Dock="Right" Margin="5,2,0,0"></TextBlock>
</StackPanel>
</Button>
</ContentControl.Content>
How do a resize btnUpdateCommand when I have localization text. I tried changing the alignment of the TextBlock etc. but no joy.
As an example this is what the button look like when it has english
And this is what the button looks like when it has french
What am I missing?
Here is the entrie XAML:
<UserControl x:Class="UpdateCheckModule.UpdateCheckProgress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Height="250" Width="500">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="175"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<ContentControl Grid.Row="0">
<ContentControl.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="95"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="23"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" Grid.Row="0" FontSize="18" />
<TextBlock Text="{Binding Status}" Grid.Row="1" />
<Border Grid.Row="2" BorderThickness="1" BorderBrush="DarkGray">
<ProgressBar Value="{Binding ProgressPercentage}" Foreground="#FF10AAE7"></ProgressBar>
</Border>
</Grid>
</ContentControl.Content>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Visible"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding HideProgress}" Value="True">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<ContentControl Grid.Row="0">
<ContentControl.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="115"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" Grid.Row="0" FontSize="18" />
<Image Source="pack://application:,,,/UpdateCheckModule;component/sedv2.ico" Grid.Column="1"></Image>
<TextBlock Text="{Binding ReleaseNotesText}" Grid.Row="1" Grid.ColumnSpan="2"/>
<Border BorderThickness="1" BorderBrush="DarkGray" Grid.Row="2" Grid.ColumnSpan="2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Text="{Binding DeploymentManifest.ReleaseNotes}" Background="White" Margin="5"></TextBlock>
</ScrollViewer>
</Border>
</Grid>
</ContentControl.Content>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding HideProgress}" Value="True">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<Button Name="btnSkip" HorizontalAlignment="Left" Grid.Row="2" Content="{Binding SkipButtonText}" Width="100" Command="{Binding SkipCommand}" Height="23">
</Button>
<ContentControl Grid.Row="2">
<ContentControl.Content>
<Button Name="btnUacRestartCommand" HorizontalAlignment="Right" Grid.Row="2" Width="175" Command="{Binding UacRestartCommand}" Height="23">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Shield}" DockPanel.Dock="Left"></Image>
<TextBlock Text="{Binding AdminInstallButtonText}" DockPanel.Dock="Right" Margin="5,2,0,0"></TextBlock>
<TextBlock Text="{Binding FormCloseCountDown}" DockPanel.Dock="Right" Margin="5,2,0,0"></TextBlock>
</StackPanel>
</Button>
</ContentControl.Content>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RequireUAC}" Value="True">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<ContentControl Grid.Row="2">
<ContentControl.Content>
<Button Name="btnUpdateCommand" HorizontalAlignment="Right" Grid.Row="2" Width="175" Command="{Binding UpdateCommand}" Height="23">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding InstallButtonText}" DockPanel.Dock="Right" Margin="5,2,0,0"></TextBlock>
<TextBlock Text="{Binding FormCloseCountDown}" DockPanel.Dock="Right" Margin="5,2,0,0"></TextBlock>
</StackPanel>
</Button>
</ContentControl.Content>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Visible"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RequireUAC}" Value="True">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
You're explicitly setting the Width of the Button (to 175). Remove this attribute; this will allow the Button to determine its own optimal width.
While you're at it, remove the DockPanel.Dock attributes on the TextBlocks; these attributes do nothing since the TextBlocks are inside a StackPanel, not a DockPanel.
i have defined a controltemplate that is used to windows ,then windows have the same view as basically.
after that ,i should add different controls in different windows. and i have no idea how to add the elements' host in controltemplate to hold different parts of controls which likes the picture bellow.
and one more thing, how to access the controls and how to set the buttons' actions in controltemplate of different window? it should be use a windowBase class to do that ?
first .the screen picture
and the control template file
<ImageSource x:Key="BtnCloseNormal">../images/others/popup_btn_reduction_normal.png</ImageSource>
<ImageSource x:Key="BtnCloseMouseOver">../images/others/popup_btn_reduction_mouseover.png</ImageSource>
<ImageSource x:Key="BtnClosePressed">../images/others/popup_btn_reduction_selected.png</ImageSource>
<Style x:Key="StatedButtonStyle" TargetType="{x:Type c:StatedButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:StatedButton}" >
<Grid>
<Border>
<Image x:Name ="btnImg" Source="{Binding NormalBackground ,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Fill"/>
</Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="btnImg" Property="Source" Value="{Binding MouseOverBackground ,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="btnImg" Property="Source" Value="{Binding PressedBackground ,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="WindowBaseStyle" TargetType="{x:Type Window}">
<Setter Property="Background" Value="{x:Null}"></Setter>
<Setter Property="AllowsTransparency" Value="True"></Setter>
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid Background="{x:Null}">
<Border CornerRadius="10" Background="White" Margin="20">
<Border.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="270" ShadowDepth="5" RenderingBias="Quality" Opacity="0.6"></DropShadowEffect>
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="40"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<Label x:Name="lblTitle" Grid.Column="0" Margin="20,6,0,0" FontSize="18" FontWeight="Bold"
Content="{TemplateBinding Title}">
</Label>
<c:StatedButton Grid.Column="2" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}"
NormalBackground="{StaticResource BtnCloseNormal}"
MouseOverBackground="{DynamicResource BtnCloseMouseOver}"
PressedBackground="{StaticResource BtnClosePressed}"/>
<c:StatedButton Grid.Column="3" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}"
NormalBackground="{StaticResource BtnCloseNormal}"
MouseOverBackground="{DynamicResource BtnCloseMouseOver}"
PressedBackground="{StaticResource BtnClosePressed}"/>
<c:StatedButton Grid.Column="4" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}"
NormalBackground="{StaticResource BtnCloseNormal}"
MouseOverBackground="{DynamicResource BtnCloseMouseOver}"
PressedBackground="{StaticResource BtnClosePressed}"/>
</Grid>
<Separator Background="LightGray" Grid.Row="1" Height="2"></Separator>
<Grid Grid.Row="2">
<!--some controls will be insert here-->
</Grid>
<Grid Grid.Row="3" Background="Red" >
<!--some controls will be insert here-->
<ContentControl Content="{Binding }"></ContentControl>
</Grid>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and the window.xaml
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:uctrls="clr-namespace:WpfApp1.Uctrls"
mc:Ignorable="d"
Style="{StaticResource WindowBaseStyle}"
Title="Window1 -- title" Height="300" Width="500">
<Grid>
<Label x:Name="lblTitle" Content="title"></Label>
<Button x:Name="BtnTest" Content="Get TextBox1" Click="BtnTestClick"></Button>
<uctrls:UserControlBase>
<Grid>
<Button Content="what "></Button>
</Grid>
</uctrls:UserControlBase>
</Grid>
i have done it myself.
just use code bellow to host new controls
<AdornerDecorator>
<contentpresenter/>
</AdornerDecorator>
and create a windowBase as parent class of new windows.
I followed a tutorial for adding a SplitView control to my page. The code looks like:
<SplitView x:Name="MainSplitView" DisplayMode="CompactOverlay" IsPaneOpen="False" CompactPaneLength="50" OpenPaneLength="150">
<SplitView.Pane>
<StackPanel Background="Gray">
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click" />
<StackPanel Orientation="Horizontal">
<Button x:Name="MenuButton1" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" />
<TextBlock Text="Button 1" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="SettingsButton" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" FontSize="18" />
<TextBlock Text="Settings" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="AboutButton" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" FontSize="18" />
<TextBlock Text="About" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
SplitView content here
</SplitView.Content>
</SplitView>
But the final result looks like this. Not really cool.
How can I reach something like the Insider Hub's?
There is a very nice example made by Justin Xin Liu on GitHub. Take a look at that for tips! ( I use same approach )
https://github.com/JustinXinLiu/SwipeableSplitView
So inside the pane use a listview like so:
<SplitView.Pane>
<ListBox ItemsSource="{x:Bind ViewModel.MenuItems}" SelectedItem="{x:Bind ViewModel.SelectedMenuItem, Mode=TwoWay, Converter={StaticResource ObjectToMenuItemConverter}}" ItemContainerStyle="{StaticResource MenuListBoxItemStyle}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="Presentation:MenuItem">
<StackPanel Orientation="Horizontal" Height="48">
<TextBlock Grid.RowSpan="2" Text="{x:Bind Icon, Mode=OneWay}" Style="{StaticResource IconTextBlockStyle}" />
<TextBlock Grid.Column="1" Text="{x:Bind Title, Mode=OneWay}" Style="{StaticResource MenuTitleTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</SplitView.Pane>
And the first TextBlock is filled with an Icon using following style
<Style x:Key="IconTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Width" Value="48" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
There is also a MenuListBoxItemStyle style for the ListBox, this will enable some animation. You can see if you want it or not.
If you want to see how I implemented it in my app, you can take a look at https://github.com/AppCreativity/Kliva. But that has an already more complex setup for the side pane, so maybe not easy to follow. On the other hand I haven't implemented the swipe guesture that Justin does in his project so maybe my app design is more like the one you want.
Using AppStudio Libraries
https://github.com/wasteam/waslibs
ShellPage
<Page
x:Name="PageLayout"
x:Class="UniversalProject.UWP.ShellPage"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:was_control="using:AppStudio.Uwp.Controls"
xmlns:controlEx="using:UniversalProject.UWP.Built_In.ControlEx"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d"
KeyUp="OnKeyUp">
<Page.Resources>
<DataTemplate x:Key="NavigationItemTemplate">
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ContentControl
Margin="12,10"
Content="{Binding Icon}"
Foreground="{StaticResource NavigationPaneText}"/>
<ContentControl
Margin="6,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding Image}" />
<TextBlock
Grid.Column="1"
Margin="16,10"
Text="{Binding Caption}"
Foreground="{StaticResource NavigationPaneText}" />
<ToolTipService.ToolTip>
<TextBlock Text="{Binding Caption}" Foreground="{ThemeResource SystemBaseHighColor}"/>
</ToolTipService.ToolTip>
</Grid>
</DataTemplate>
<Style x:Key="NavigationSeparatorStyle" TargetType="ContentControl">
<Setter Property="Margin" Value="8,0" />
<Setter Property="MinHeight" Value="7"/>
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Line
X1="0"
X2="1"
Stretch="UniformToFill"
Stroke="{StaticResource NavigationPaneText}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="DefaultHeaderTemplate">
<Grid Background="Transparent">
<TextBlock
Margin="14,8"
Text="{Binding}"
Style="{StaticResource AppBarTitleStyle}" />
</Grid>
</DataTemplate>
<Style x:Key="ListViewItemContainerStyle" TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Padding" Value="2,0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
ContentTransitions="{TemplateBinding ContentTransitions}"
SelectionCheckMarkVisualEnabled="True"
CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
FocusBorderBrush="{StaticResource NavigationPaneText}"
FocusSecondaryBorderBrush="{StaticResource NavigationPaneText}"
PlaceholderBackground="Transparent"
PointerOverForeground="{StaticResource NavigationPaneText}"
SelectedForeground="{StaticResource NavigationPaneText}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="Inline">
<ListViewItemPresenter.SelectedBackground>
<SolidColorBrush Color="{StaticResource SystemAccentColor}" Opacity="0.6"/>
</ListViewItemPresenter.SelectedBackground>
<ListViewItemPresenter.SelectedPressedBackground>
<SolidColorBrush Color="{StaticResource SystemAccentColor}" Opacity="1.0"/>
</ListViewItemPresenter.SelectedPressedBackground>
<ListViewItemPresenter.SelectedPointerOverBackground>
<SolidColorBrush Color="{StaticResource SystemAccentColor}" Opacity="0.8"/>
</ListViewItemPresenter.SelectedPointerOverBackground>
<ListViewItemPresenter.PointerOverBackground>
<SolidColorBrush Color="{StaticResource SystemAccentColor}" Opacity="0.1"/>
</ListViewItemPresenter.PointerOverBackground>
<ListViewItemPresenter.PressedBackground>
<SolidColorBrush Color="{StaticResource SystemAccentColor}" Opacity="0.3"/>
</ListViewItemPresenter.PressedBackground>
</ListViewItemPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid x:Name="MainLayout" Background="{StaticResource AppBackground}">
<was_control:ShellControl
x:Name="shell"
Header="App Name"
NavigationItems="{Binding NavigationItems}"
BorderBrush="{StaticResource AppBarBackground}"
HeaderBackground="{StaticResource HeaderAppbarBackground}"
HeaderForeground="{StaticResource AppBarForeground}"
HeaderTemplate="{StaticResource DefaultHeaderTemplate}"
NavigationForeground="{StaticResource NavigationPaneText}"
SeparatorStyle="{StaticResource NavigationSeparatorStyle}"
NavigationBackground="{StaticResource NavigationPaneBackground}"
HamburgerBackground="{StaticResource SystemControlBackgroundAccentBrush}"
HamburgerBorderBrush="{StaticResource NavigationPaneButton}"
HamburgerForeground="{StaticResource NavigationPaneText}"
NavigationItemTemplate="{StaticResource NavigationItemTemplate}"
NavigationSubItemTemplate="{StaticResource NavigationItemTemplate}"
ListViewItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
<was_control:ShellControl.CommandBar>
<CommandBar x:Name="commandbar" RequestedTheme="Dark">
<CommandBar.SecondaryCommands>
<controlEx:SecondaryIconButton Glyph="" RequestedTheme="Dark"
Foreground="{StaticResource NavigationPaneText}"
x:Name="createButton"
x:Uid="CreateNewItemLabel"></controlEx:SecondaryIconButton>
<controlEx:SecondaryIconButton Glyph="" RequestedTheme="Dark"
Foreground="{StaticResource NavigationPaneText}"
x:Name="importExportButton"
x:Uid="ImportExportLabel" ></controlEx:SecondaryIconButton>
</CommandBar.SecondaryCommands>
<CommandBar.PrimaryCommands>
</CommandBar.PrimaryCommands>
</CommandBar>
</was_control:ShellControl.CommandBar>
<Grid x:Name="contentgrid" Margin="0,0,0,0">
<Frame x:Name="frame" />
</Grid>
</was_control:ShellControl>
</Grid>
The modern way of doing this is using NavigationView. It looks great by default and requires less messing around to implement. It's built-in to the OS since build 16299 and available in WinUI 2 for downlevel support.
I want to create a notification window using Mahapps.Metro. I defined a new Style for MetroWindow but when Show() is executed, application throws a null exception. If i remove the template override, i have no problem... Any ideas?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:MyApp.Controls"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls">
<Style x:Key="ErrorPopup" TargetType="{x:Type Controls:MetroWindow}" BasedOn="{StaticResource {x:Type Controls:MetroWindow}}">
<Setter Property="BorderBrush" Value="{StaticResource ErrorPopupBorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="300"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:MetroWindow}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Grid.Row="0" Grid.RowSpan="3">
<Rectangle.Fill>
<SolidColorBrush Color="Red"/>
</Rectangle.Fill>
</Rectangle>
<Button Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" Content="X" Padding="0,0,0,0"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Error Title" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<ContentPresenter Grid.Row="2" Grid.Column="1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<Controls:MetroWindow x:Class="MyApp.Controls.NotificationPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Style="{StaticResource ErrorPopup}" ButtonBase.Click="Button_Clicked" Title="Notification Title">
<Controls:MetroWindow.Content>
<TextBlock x:Name="_textContainer" Style="{StaticResource NotificationText}" Text="This is the text to display" TextAlignment="Left" TextWrapping="Wrap"/>
</Controls:MetroWindow.Content>
</Controls:MetroWindow>
I have the following HeaderTemplate for an Expander:
<Expander.HeaderTemplate>
<DataTemplate>
<Grid Background="#939393">
<Grid.RowDefinitions>
<RowDefinition Height="22"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" CornerRadius="5,5,5,5"
Background="Transparent" BorderBrush="#FF000000" Margin="1" BorderThickness="1,1,1,1" SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" TargetName="border" Value="DarkGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TextBlock Grid.Column="0" Background="#6E6E6E"/>
<ToggleButton Grid.Column="0" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" Focusable="False">
<Image Source="{Binding IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}, Converter={StaticResource boolToExpanderDirectionConverter}}"/>
</ToggleButton>
<TextBlock Grid.Column="1" Text="General" Margin="5,1,1,1" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
</DataTemplate>
</Expander.HeaderTemplate>
This Headertemplate I have defined directly at one Expander. Now I want to move this Template to a Resource and apply it to all Expanders. My problem now is, that I don't know how to set the Header of the TextBlock in the Template to the Header of the Expander.
I knwo there's a way with TemplateBinding, but unfortunately I don't know how to use this.
TemplateBinding can only be used within a ControlTemplate.TemplateBinding is used for binding to the element properties within the template definition..
here in your example you have used controltemplate for toggleButton.
Example For TemplateBinding
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter TextElement.Foreground="{TemplateBinding Foreground}" x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
.....
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ToggleButton Grid.Column="0" Background="Red" BorderBrush="Black" BorderThickness="1" Foreground="White"/>
Here Border and Contentpresnter will bind properties of ToggleButton that is alreday defined in its defination.
But in your example you used Datatemplate..so you cant use TemplateBinding ..Please Follow This link for Binding Syntax.
Solution For Your example
Using Binding syntax we can bind Header property to different exapnder
Text="{Binding Path=Header,RelativeSource={RelativeSource AncestorType={x:Type Expander}}}"
<Window.Resources>
<DataTemplate x:Key="ExpanderHeaderTemplate">
<Grid Background="#939393">
<Grid.RowDefinitions>
<RowDefinition Height="22"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" CornerRadius="5,5,5,5"
Background="{TemplateBinding Background}" BorderBrush="#FF000000" Margin="1" BorderThickness="1,1,1,1" SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" TargetName="border" Value="DarkGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TextBlock Grid.Column="0" Background="#6E6E6E"/>
<ToggleButton Grid.Column="0" Background="Red" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" Focusable="False">
<Image Source="{Binding IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}, Converter={StaticResource boolToExpanderDirectionConverter}}"/>
</ToggleButton>
<TextBlock Grid.Column="1" Text="{Binding Path=Header,RelativeSource={RelativeSource AncestorType={x:Type Expander}}}" Margin="5,1,1,1" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Expander Header="General1" HeaderTemplate="{StaticResource ExpanderHeaderTemplate}"/>
<Expander Header="General2" HeaderTemplate="{StaticResource ExpanderHeaderTemplate}"/>
</StackPanel>
you can have a idea of Template binding by below code i had implemented this when i was learning XAML.
<Button Template="{DynamicResource CircleButton}" Background="Green" Content="1"></Button>
<ControlTemplate x:Key="CircleButton" TargetType="{x:Type Button}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" MinHeight="36" MinWidth="36">
<Ellipse Fill="{TemplateBinding Background}"></Ellipse>
<ContentPresenter TextBlock.FontFamily="Calibri" TextBlock.FontSize="24" TextBlock.Foreground="Wheat" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
you have to give a x:Key to control Template and when you are binding it with some specific element define that x:key there like i did Template="{DynamicResource CircleButton}".
following is your case:
<Style TargetType="{x:Type Expander}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
// Do your thing.
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
this will apply to all Expander throught your application. You can put this Style into App.xaml file for code cleanness. hope this'll help you.
Since you are specifying a HeaderTemplate via a DataTemplate, the DataContext of the template IS the Header. This simple example works:
<Expander Header="Test">
<Expander.HeaderTemplate>
<TextBlock Text="{Binding}"/>
</Expander.HeaderTemplate>
</Expander>