C# UWP Style change programmatically - c#

I have a button with Resource style. How to change Text of TextBlock in Button Content?
Here is the style:
<Style x:Key="NavigationLogoutButtonStyle" TargetType="Button" BasedOn="{StaticResource NavigationBackButtonNormalStyle}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Height" Value="48"/>
<Setter Property="Width" Value="NaN"/>
<Setter Property="MinWidth" Value="48"/>
<Setter Property="AutomationProperties.Name" Value="Logout"/>
<Setter Property="Content">
<Setter.Value>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<FontIcon Grid.Column="0" FontSize="16" Glyph="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<StackPanel Grid.Column="1" Orientation="Vertical">
<TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="!!!TEXT HERE PROGRAMMATICALLY!!!" Foreground="{StaticResource MainColorBrush}" FontSize="13" VerticalAlignment="Center" />
<TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="{StaticResource LogoutButtonText}" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Setter.Value>
</Setter>
</Style>

Related

ListView Virtualization with FlowDocument as DataTemplate

As a self-set himself the task of writing the messenger (like Telegram, Viber, etc). To display the messages in the chat, I use ListView with FlowDocument as ItemTemplate . This control was chosen because:
1) It is necessary to display images (emoticons);
2) Hyperlinks;
3) It should be possible to select text to copy.
But the application consumes lots of memory, a feeling that FlowDocument not deleted from the memory.
It's my View:
<CollectionViewSource x:Key="ItemListViewSource"
IsLiveSortingRequested="True"
Source="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
Path=Tag.Messages}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="date" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<Style x:Key="lvItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="White" />
<Setter Property="Padding" Value="0" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderThickness="0"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="lvStyle" TargetType="{x:Type ListView}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="White" />
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="VirtualizingPanel.IsVirtualizing" Value="True" />
<Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel" />
<Setter Property="Margin" Value="0" />
<Setter Property="ItemContainerStyle" Value="{StaticResource lvItemStyle}" />
<Setter Property="Padding" Value="0" />
</Style>
<DataTemplate x:Key="MessageTemplate">
<Grid Margin="0,7,0,7">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
Width="40"
Height="40"
Margin="5,0,10,0"
VerticalAlignment="Top"
Source="{Binding Converter={StaticResource getAvatarForChat},
Path=fromId,
Mode=OneWay,
ConverterParameter=40}">
<Image.Clip>
<EllipseGeometry Center="20,20"
RadiusX="20"
RadiusY="20" />
</Image.Clip>
</Image>
<TextBlock Grid.Row="0"
Grid.Column="1"
Margin="0"
VerticalAlignment="Top"
FontSize="13"
FontWeight="SemiBold"
Padding="0"
Text="{Binding chatTitle, Mode=OneWay" />
<TextBlock Grid.Row="0"
Grid.Column="2"
Margin="10,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="13"
FontWeight="ExtraLight"
Padding="0"
Text="{Binding Path=date,
Converter={StaticResource dateConverter},
ConverterParameter=HH:mm,
Mode=OneWay}" />
<FlowDocumentScrollViewer Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="0,3,0,0"
Document="{Binding message,
Converter={StaticResource documentConverter},
Mode=OneWay}"
Padding="0"/>
</Grid>
</DataTemplate>
<ListView x:Name="listView"
Grid.Row="0"
ItemsSource="{Binding Source={StaticResource ItemListViewSource},
NotifyOnTargetUpdated=True}""
ItemTemplate="StaticResource MessageTemplate"
Style="{StaticResource lvStyle}">
</ListView>
Please help me understand this problem, and I'm sorry for my english!

TextBox's error validation animation is too slow

I'm using Mahaaps.Metro AnimatedTabControl. When I switch from one tab to another, I can see the slow transition on AdornedElementPlaceholder. Here's a screenshot:
I am using MahApps metro animated tab transition.
XAML:
<TextBox Style="{StaticResource EditValueStyle}" Visibility="{Binding ManualDimmingVisibility}" Text="{Binding ManualDampingValue, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" />
<TextBox Style="{StaticResource EditValueStyle}" Text="{Binding SignalIntensity, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" Height="22" />
<Style x:Key="EditValueStyle" TargetType="TextBox">
<Setter Property="Control.HorizontalAlignment" Value="Left" />
<Setter Property="Control.VerticalAlignment" Value="Center" />
<Setter Property="Control.Height" Value="Auto" />
<Setter Property="Control.HorizontalContentAlignment" Value="Right" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
<Setter Property="Control.Foreground" Value="#333" />
<Setter Property="Control.Width" Value="60" />
<Setter Property="Control.FontSize" Value="14" />
<Setter Property="MaxLength" Value="8"></Setter>
<Setter Property="Control.Margin" Value="0,0,15,0" />
<Setter Property="Control.FontFamily" Value="Segoe UI Symbol" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right" Margin="3,0,0,0" Width="15" Height="15" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
</TextBlock>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You should surround your content with an AdornerDecorator to avoid this issue.
<AdornerDecorator>
<!-- Your content -->
</AdornerDecorator>
And, maybe you forget to inherit the TextBox style from MahApps style.
<Style x:Key="EditValueStyle" TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}">
<!-- Your custom style changes -->
</Style>

Toggle button style with text and image

I have a next style:
<Style x:Key="Style_MainButtons" TargetType="ToggleButton">
<Setter Property="Width" Value="110" />
<Setter Property="Height" Value="110" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock x:Name="Text" HorizontalAlignment="Center" />
<Image x:Name="Image" Grid.Row="1" />
</Grid>
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Because there are few toggle buttons using this style and each have different text and image, so how can I call “TextBlock” and “Image” from style in the next code?
<ToggleButton x:Name="btnHorizontal"
Style="{StaticResource Style_MainButtons}"
Grid.Column="0"
Grid.Row="1"
Checked ="SetVersion"
Unchecked ="SetVersion"
IsChecked="True" >
</ToggleButton>
You can do something like this:
Style
<Style TargetType="ToggleButton">
<Setter Property="Width" Value="110" />
<Setter Property="Height" Value="110" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock x:Name="Text" HorizontalAlignment="Center" Text="{TemplateBinding Content}"/>
<Image x:Name="Image" Grid.Row="1" Source="{TemplateBinding Tag}" />
</Grid>
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Usage
<ToggleButton Content="Text" Tag="{Binding Path=Source, Source={StaticResource Image}}"></ToggleButton>
<ToggleButton Content="Another Text" Tag="{Binding Path=Source, Source={StaticResource Image}}"></ToggleButton>
Note: Replace my Tag-Binding with an valid Binding to your actual ImageSource

Base Expander Style, override header color

Is it possible to create some sort of base expander style and override the background color of the header in a derived style?
In my application I'm using expanders a lot and I would like to change the background color of the header. I know I could just copy&paste my style and edit the color, but it would be nicer to just create a new style based on the "base style" and setting the header's background color.
But I do not know how to access this color.
It's the color of this line: below I'd like to change (the border in the header): Border Name="border"... I cannot access "border" in the setter of the derived style...
This is my (base) style:
<Style TargetType="Expander" x:Key="ExpanderStyle">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextColor}}"/>
<Setter Property="Template">
<Setter.Value>
<!-- Control template for expander -->
<ControlTemplate TargetType="Expander" x:Name="exp">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Name="ContentRow" Height="0"/>
</Grid.RowDefinitions>
<Border Name="border" Grid.Row="0" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" BorderThickness="1" CornerRadius="4,4,0,0" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="tb" FontFamily="Marlett" FontSize="9.75" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Foreground="Black" Grid.Column="1" Content="u" IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter x:Name="HeaderContent" Grid.Column="0" Margin="4" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
<Border x:Name="Content" Grid.Row="1" BorderThickness="1,0,1,1" CornerRadius="0,0,4,4" >
<ContentPresenter Margin="4" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content,Path=Height}" />
<Setter Property="Content" TargetName="tb" Value="t"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to do something like this:
<Style x:Key="ExpanderStyleRed" BasedOn="{StaticResource ExpanderStyle}" TargetType="Expander">
<Setter Property="???" Value="Red"/>
<Style>
Use TemplateBinding:
<Style TargetType="Expander" x:Key="ExpanderStyle">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextColor}}"/>
<Setter Property="Template">
<Setter.Value>
<!-- Control template for expander -->
<ControlTemplate TargetType="Expander" x:Name="exp">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Name="ContentRow" Height="0"/>
</Grid.RowDefinitions>
<Border Name="border" Grid.Row="0" Background="{TemplateBinding Background}" BorderThickness="1" CornerRadius="4,4,0,0" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="tb" FontFamily="Marlett" FontSize="9.75" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Foreground="Black" Grid.Column="1" Content="u" IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter x:Name="HeaderContent" Grid.Column="0" Margin="4" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
<Border x:Name="Content" Grid.Row="1" BorderThickness="1,0,1,1" CornerRadius="0,0,4,4" >
<ContentPresenter Margin="4" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content,Path=Height}" />
<Setter Property="Content" TargetName="tb" Value="t"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExpanderStyleRed" BasedOn="{StaticResource ExpanderStyle}" TargetType="Expander">
<Setter Property="Background" Value="#2fff0000"/>
</Style>
And then:
<Grid>
<Expander x:Name="expander1" Style="{DynamicResource ExpanderStyle}" Header="Expander" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="7,10,0,0" Height="108">
<TextBlock Width="250" Height="150" TextWrapping="Wrap">
asklsaklsa saaskklsaklas alsaklalkjd
asklsaklsaklsa saklsaklsakl jsajkjska
saklsaklsakl sasa
</TextBlock>
</Expander>
<Expander x:Name="expander2" Style="{DynamicResource ExpanderStyleRed}" Header="Expander" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="12,126,0,0" Height="133">
<TextBlock Width="250" Height="150" TextWrapping="Wrap">
asklsaklsa saaskklsaklas alsaklalkjd
asklsaklsaklsa saklsaklsakl jsajkjska
saklsaklsakl sasa
</TextBlock>
</Expander>
</Grid>

Can you make a style with Buttons like the ones in the picture with an text over the image marked with a color?

I am wondering if its possible in WPF to make a Button style where you have a text, an image and text over the image (upon the image) with a color mark as illustrated on the picture below?
Until now i only got this
<Style TargetType="{x:Type Button}" x:Key="CatProBtn">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<!--<Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0" Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>-->
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
HorizontalAlignment="Right" VerticalAlignment="Center"
/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<SoundPlayerAction Source="C:\Users\shaban\Downloads\Cash_register_beep_sound_.wav" />
</EventTrigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E59400" />
<Setter Property="Foreground" Value="White" />
<!--<Setter TargetName="PathIcon" Property="Fill" Value="Black" />-->
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="OrangeRed" />
<Setter Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Wheat"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
....
<Button
Style="{StaticResource CatProBtn}"
Margin="1,1,0,3"
Name="Shaban"
Height="Auto"
Width="Auto"
Grid.Row="1"
Grid.Column="1"
Click="Button_Click">
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center">
<TextBlock Text="Ispinde"></TextBlock>
<Image Source="C:\Users\shaban\Pictures\Picture5.png" Stretch="Uniform"></Image>
<TextBlock Text="Ispinde" HorizontalAlignment="Right"/>
</StackPanel>
</Button>
Inspire from code like this in your Button and modify the values to achieve your desired result.
Use a Grid to layout your content. Superimpose the price on the Image.
<Button MinHeight="50" HorizontalAlignment="Center">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Title" TextAlignment="Center"/>
<!-- This is where your image is in your code -->
<TextBlock Grid.Row="1" Text="Your
image
here..." FontSize="14" Background="Gray" TextAlignment="Center"/>
<TextBlock Grid.Row="2" Text="Description Description Description" TextAlignment="Center" TextWrapping="Wrap" FontSize="8" MaxWidth="100"/>
<!-- Superimpose price on top of image with some opacity -->
<TextBlock Grid.Row="1" Text="20,00 kr." Background="Yellow" Foreground="Red" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,12" Opacity="0.6" FontSize="8"/>
</Grid>
</Button>
Result
You can overlay a Control over another Control by using Grids. It will be something like:
<Button>
<Grid>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center">
<TextBlock Text="Ispinde"/>
<Image Source="C:\Users\shaban\Pictures\Picture5.png"" Stretch="Uniform"/>
</StackPanel>
<Grid Width="100" Height="40" HorizontalAlignment="Right">
<Grid Background="Red" Opacity="0.6"/>
<TextBlock Foreground="White" Text="Overlay"/>
</Grid>
</Grid>
</Button>

Categories