WPF: I don't want to pass Style to Child Elements - wpf

I have a UserControl that whose Root Element is a Grid which is supposed to take a Style with Red Background Color and an Opacity Animation that makes it flash. There are two Child Grids inside that Root Grid. They are supposed to keep their original Color (and all their Children too), which is working fine. But I don't want them to get the Animation, which they currently do. So the whole UI is flashing right now, but I only want the parts that turn Red to flash, the Rest should not flash. I already tried to apply an empty Style to the Child Grids and also tried a Style with a fixed Opacity of 1. Both didn't work. Here's my XAML:
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Grid}" x:Key="AdminModeStyle">
<Style.Resources>
<Storyboard x:Key="flashAnimation" >
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
</Storyboard>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding IsAdminMode}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimation}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="flash"/>
</DataTrigger.ExitActions>
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsAdminMode}" Value="False">
<Setter Property="Background" Value="{StaticResource PrimaryBackground}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type Grid}" x:Key="NoStyle">
<Setter Property="Opacity" Value="1"></Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid Style="{StaticResource AdminModeStyle}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Style="{StaticResource NoStyle}" Grid.Column ="0" Grid.Row ="1">
....Content
</Grid>
<Grid Style="{StaticResource NoStyle}" Grid.Column ="1" Grid.Row ="1" >
....Content
</Grid>
</Grid>
Can you please help me?

Opacity is automatically applied to all child items. To fix this, you need to place your content grid on top of the animated grid and not nested inside of it.
<Grid>
<Grid Style="{StaticResource AdminModeStyle}" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid
Grid.Row="1"
Grid.Column="0">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="This is a test..." />
</Grid>
<Grid
Grid.Row="1"
Grid.Column="1">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="This is a test..." />
</Grid>
</Grid>
</Grid>

Related

How do I combine PreviewMouseLeftButtonDown with Command?

In sample project I have a button binded to a Command. It works as button-click event.
However, I would like to use PreviewMouseLeftButtonDown/Up event in in conjunction with Command.
For example LeftButtonDown - filling with green, LeftButtonUp -default filling.
Is there a neat way to do that?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button Command="{Binding ColourChange_command, Mode=OneWay}" Content="Fill with green" />
</StackPanel>
<Ellipse Grid.Column="1" Stroke="Black">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Triggers>
<DataTrigger Binding="{Binding ColourChange}" Value="True">
<Setter Property="Fill" Value="LimeGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
</Grid>

How to change the borderbrush on a Mouse Over event in WPF

I am trying to change the color of the BorderBrush when the mouse is hovering over the button. I have created a new control template for the button however when setting up the triggers visual studio tells me i have got to use an EventTrigger but when i use this there is no MouseOver event, only a MouseEnter event. When applying this and running the solution the border brush does not change. Any Suggestions?
EDIT:
Code I am having the issue with:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<ControlTemplate x:Key="LogInButton">
<Grid Width="AUTO" Height="AUTO">
<Border x:Name="ButtonBorder"
BorderBrush="#B7B7B7"
BorderThickness="2"
Background="Transparent"
Cursor="Hand">
<Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
<Border.Triggers>
<EventTrigger RoutedEvent="MouseMove">
<EventTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBorder"
Storyboard.TargetProperty="BorderBrush"
To="White"
Duration="0"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.EnterActions>
</EventTrigger>
</Border.Triggers>
</Border>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource LogInButton}" Tag="Images/Login.png" Height="50" Width="50" Background="Transparent">
</Button>
</Grid>
You could just add a Trigger to <ControlTemplate.Triggers> instead of adding an EventTrigger to <Border.Triggers>:
<ControlTemplate x:Key="LogInButton">
<Grid Width="AUTO" Height="AUTO">
<Border x:Name="ButtonBorder"
BorderBrush="#B7B7B7"
BorderThickness="2"
Background="Transparent"
Cursor="Hand">
<Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Change FontSize as per Window size In Data Template in Windows 10 Universal App

I used a Data template and use that Data Template in the List view.i also use a Visual State Trigger for increase and decrease font size so my question is that when the windows state trigger but font size not changed. font size remain same in both mobile view in PC view.
Its my Code:
Data Template:
<Page.Resources>
<DataTemplate x:Name="CustomerListViewTemplate" x:DataType="data:Customerlistdata">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{x:Bind full_firstname}"
x:Phase="1"
x:Name="product_name_layout"
Style="{ThemeResource BaseTextBlockStyle}"
TextWrapping="NoWrap"
Foreground="Gray"
Margin="15,10,0,0"
/>
<TextBlock
Grid.Row="1"
Text="{x:Bind email_id}"
x:Phase="2"
Style="{ThemeResource BodyTextBlockStyle}"
Margin="15,5,0,10"
x:Name="email_id"
FontSize="12"
Foreground="Gray"/>
</Grid>
</DataTemplate>
</Page.Resources>
List View:
<ListView x:Name="MasterListView"
SelectionMode="Extended"
UseLayoutRounding="False"
ScrollViewer.VerticalScrollMode="Enabled"
BorderBrush="#FFA79E9E"
SelectionChanged="OnSelectionChanged"
IsItemClickEnabled="True"
ShowsScrollingPlaceholders="False"
ItemTemplate="{StaticResource CustomerListViewTemplate }"
ItemClick="OnItemClick"
Grid.Column="0"
Grid.Row="1"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Visual State:
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="MasterListView.Visibility" Value="Visible" />
<Setter Target="DetailContentPresenter.Visibility" Value="Collapsed" />
<Setter Target="MasterColumn.Width" Value="*" />
<Setter Target="DetailColumn.Width" Value="0"/>
<Setter Target="product_name_layout.FontSize" Value="10"/>
</VisualState.Setters>
</VisualState>
so give me suggestion how can i change font size as per the windows size.
i Want to change a Font Size on window size change. i used a built in style in that.Actully I am making a master detail View in windows universal app .all functionality work properly but font size is not changed.
In this situation we can not change the font size of the data template so we need to add two data template
1) data template for pc
2) data template for mobile
for pc:
--------------------------------------------------------------------------
<Page.Resources>
<DataTemplate x:Name="CustomerListViewTemplatePC" x:DataType="data:Customerlistdata">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{x:Bind full_firstname}"
x:Phase="1"
x:Name="product_name_layout"
Style="{ThemeResource BaseTextBlockStyle}"
TextWrapping="NoWrap"
Foreground="Gray"
Margin="15,10,0,0"
/>
<TextBlock
Grid.Row="1"
Text="{x:Bind email_id}"
x:Phase="2"
Style="{ThemeResource BodyTextBlockStyle}"
Margin="15,5,0,10"
x:Name="email_id"
FontSize="12"
Foreground="Gray"/>
</Grid>
</DataTemplate>
</Page.Resources>
-------------------------------------------------------------------------------
for mobile
<Page.Resources>
<DataTemplate x:Name="CustomerListViewTemplateMobile" x:DataType="data:Customerlistdata">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{x:Bind full_firstname}"
x:Phase="1"
x:Name="product_name_layout"
Style="{ThemeResource BaseTextBlockStyle}"
TextWrapping="NoWrap"
Foreground="Gray"
Margin="15,10,0,0"
/>
<TextBlock
Grid.Row="1"
Text="{x:Bind email_id}"
x:Phase="2"
Style="{ThemeResource BodyTextBlockStyle}"
Margin="15,5,0,10"
x:Name="email_id"
FontSize="12"
Foreground="Gray"/>
</Grid>
</DataTemplate>
</Page.Resources>
-----------------------------------------------------------------
aftre make a two individual data template then fire a trigger for both mobile(narrow state) and pc(wide state)
like this...........
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PageSizeStatesGroup"
CurrentStateChanged="OnCurrentStateChanged">
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="MasterListView.Visibility" Value="Visible" />
<Setter Target="DetailContentPresenter.Visibility" Value="Collapsed" />
<Setter Target="MasterColumn.Width" Value="*" />
<Setter Target="DetailColumn.Width" Value="0"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="WideState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="MasterListView.Visibility" Value="Visible" />
<Setter Target="DetailContentPresenter.Visibility" Value="Collapsed" />
<Setter Target="MasterColumn.Width" Value="*" />
<Setter Target="DetailColumn.Width" Value="auto"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
-----------------------------------------------------------------------------
then apply data template in the listview like this
<ListView x:Name="MasterListView"
SelectionMode="Extended"
UseLayoutRounding="False"
ScrollViewer.VerticalScrollMode="Enabled"
BorderBrush="#FFA79E9E"
SelectionChanged="OnSelectionChanged"
IsItemClickEnabled="True"
ShowsScrollingPlaceholders="False"
ItemTemplate="{`enter code here`StaticResource CustomerListViewTemplate }"
ItemClick="OnItemClick"
Grid.Column="0"
Grid.Row="1"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="FontSize" Value="10" />
</Style>
</ListView.ItemContainerStyle>
</ListView>

Change Grid Panel Border color Using C# programatically- WPF

I have Grid Panel and I want to its border color to change when we click on button
<Grid Grid.Row="3" Name="LocationLayoutPanel" VerticalAlignment="Top"
Margin="0,51,0,0" Height="65" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="9*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions >
<Border BorderThickness="2,2,2,2" BorderBrush="Red" Grid.ColumnSpan="3"
Grid.RowSpan="2"/>
<Button Content="change border color" Grid.Row="1" Grid.Column="1"
Click="chnageBGCOLOR"></Button>
</Grid>
When I click on "change border color" button its grid border color should be changed!
Thank you!
XAML:
<Border BorderThickness="2,2,2,2" Grid.ColumnSpan="3"
Grid.RowSpan="2">
<Border.Style>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Button, Path=IsPressed}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
To="GreenYellow"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
<Button Name="Button" Content="change boarder color" Grid.Row="1" Grid.Column="1"></Button>
Programically:
Add Name to Border and set as follows:
BorderName.BorderBrush = new SolidColorBrush(Colors.GreenYellow);

Need some suggestions about how to reference outside control

I insert a Tabcontrol into a BorderControl. The Tabcontrol used seperate styles. Right now the Close Button inside Tabcontrol can refer to itself. So when click it the TabControl is closed I guess so. Is it possible to refer the button to the Border control outside so that when close the Border control is collpased?
<DockPanel LastChildFill="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" MinWidth="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Margin="5,5,0,5" BorderBrush="Aqua" BorderThickness="1">
<TabControl Margin="0,15,0,0" Style="{StaticResource StandardTabControl}" >
<TabItem Header="Start">
</TabItem>
</TabControl>
</Border>
<GridSplitter Grid.Column="1" Background="Transparent" Width="6" HorizontalAlignment="Center"></GridSplitter>
<Border Grid.Column="2" BorderBrush="Aqua" BorderThickness="1" Margin="0,5,5,5"></Border>
</Grid>
</DockPanel>
After click the close button
And part of the styles of TabControl.
<Button Grid.Column="1" Height="15" Width="15" HorizontalAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Ac:CloseTabItemAction TabItem="{Binding RelativeSource={RelativeSource AncestorType=TabItem}}"
TabControl="{Binding RelativeSource={RelativeSource AncestorType=TabControl}}">
</Ac:CloseTabItemAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<!--A lot more about styles-->
</Button>
here is a solution using control template triggers in pure xaml
<DockPanel LastChildFill="True">
<Control>
<Control.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"
MinWidth="100"
x:Name="column" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Margin="5,5,0,5"
BorderBrush="Aqua"
BorderThickness="1">
<TabControl Margin="0,15,0,0"
Style="{StaticResource StandardTabControl}"
x:Name="tabControl">
<TabItem Header="Start">
</TabItem>
</TabControl>
</Border>
<GridSplitter Grid.Column="1"
Background="Transparent"
Width="6"
HorizontalAlignment="Center"></GridSplitter>
<Border Grid.Column="2"
BorderBrush="Aqua"
BorderThickness="1"
Margin="0,5,5,5"></Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems,ElementName=tabControl}"
Value="false">
<Setter TargetName="column"
Property="MinWidth"
Value="0" />
<Setter TargetName="column"
Property="Width"
Value="0" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
</DockPanel>
based on the assumption that when CloseTabItemAction will be called it will remove the TabItem from the TabControl, the trigger will detect the same based on HasItems property and will collapse the column if there are no items
EDIT
based on the comments here are the finding. while resizing GridSplitter sets a Local value to the ColumnDefinition's Width property which have higher precedence than the Trigger values so the Trigger effectively fails to modify the value.
the solution proposed is to use Animation to set the desired value as Animations has higher precedence then the Local value, and the desired value is applied to the property.
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems,ElementName=T1}"
Value="false">
<Setter TargetName="Co0"
Property="MinWidth"
Value="0" />
<Setter TargetName="G1"
Property="Width"
Value="0" />
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Co0"
Storyboard.TargetProperty="Width">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<GridLength>0</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</ControlTemplate.Triggers>
in the above code I also make the splitter width to 0 so that there is no empty space on the left

Resources