'Get the the source from ToggleButton contained into a style
I have multiple expanders using a style. Into the style is defined a toggle button, and when I click on It I need to detect which off the togglebutton have been clicked.
expander 1:
<Expander Style="{StaticResource PreviewExpanderNewGeneration}" VerticalAlignment="Top" Grid.ColumnSpan="2" Name="expander1" OverridesDefaultStyle="True" Header="Expander1Header" Margin="0,0,0,0">
<GroupBox Height="150" FontStyle="Normal" utilities:GroupNameUpperConverter.CharacterCasing="Upper" Name="ContainerforExpander1" Background="#717171" Margin="0,0,0,0" >
</GroupBox>
</Expander>
expander 2:
<Expander Style="{StaticResource PreviewExpanderNewGeneration}" VerticalAlignment="Top" Grid.ColumnSpan="2" Name="expander2" OverridesDefaultStyle="True" Header="Expander2Header" Margin="0,0,0,0">
<GroupBox Height="150" FontStyle="Normal" utilities:GroupNameUpperConverter.CharacterCasing="Upper" Name="ContainerforExpander2" Background="#717171" Margin="0,0,0,0" >
</GroupBox>
</Expander>
the style define contains a Togglebutton who change graphically when clicked on. But I want to know which of the togglebutton from the expanders have been clicked: XAML
<Style TargetType="Expander" x:Key="PreviewExpanderNewGeneration" BasedOn="{StaticResource BaseControlStyle}" >
<!--x:Key="GroupBoxStyle"-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
<Grid>
<!--Grid Rows split the GroupBox into two areas -->
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<!--Header area-->
<Border Name="HeaderArea"
Grid.Row="0"
Background="Transparent"
VerticalAlignment="Center"
BorderBrush="Transparent"
BorderThickness="1"
CornerRadius="2,2,0,0" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="67"/>
<ColumnDefinition Width="45"/>
<ColumnDefinition Width="95"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.ColumnSpan="3" >
<Label Foreground="White" Grid.Column="0" utilities:LabelUpperCase.CharacterCasing="Upper" Name="PreviewLabelExpander" FontSize="15" HorizontalAlignment="Left" Loaded="PreviewLabelExpander_Loaded" FontWeight="Normal" Content="{TemplateBinding Header}" ></Label>
<StackPanel VerticalAlignment="Center">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding IsHelp}" Value="true">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsHelp}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
</StackPanel>
<ToggleButton
Grid.Row="0"
Grid.Column="3"
Margin="79,4,0,4"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Template="{StaticResource PreviewToggleButton}"
OverridesDefaultStyle="True"
Click="PreviewExpanderButton_Click"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
Width="20">
</ToggleButton>
</Grid>
</Border>
<!-- Main client area -->
<Border Name="ContentArea"
Grid.Row="1"
Height="100"
BorderBrush="Transparent"
BorderThickness="1,1,1,1"
CornerRadius="6,5,6,6" >
<ContentPresenter x:Name="ExpanderContent"
VerticalAlignment="Top"
Height="100"
Margin="0,0,0,0" >
<ContentPresenter.LayoutTransform>
<ScaleTransform ScaleY="0"></ScaleTransform>
</ContentPresenter.LayoutTransform>
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ExpanderContent"
Storyboard.TargetProperty="(ContentPresenter.LayoutTransform).(ScaleTransform.ScaleY)"
To="1"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ExpanderContent"
Storyboard.TargetProperty="(ContentPresenter.LayoutTransform).(ScaleTransform.ScaleY)"
To="0"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and now the click event should detect the source but how?
private void PreviewExpanderButton_Click(object sender, RoutedEventArgs e)
{
//how to detect which of the toogle button have been clicked?
ToggleButton tmp = (ToggleButton) sender;
}
Solution 1:[1]
If you want to get a reference to the parent Expander
of the ToggleButton
that was clicked, you could use the following helper method:
private void PreviewExpanderButton_Click(object sender, RoutedEventArgs e)
{
ToggleButton tmp = (ToggleButton)sender;
Expander expander = FindParent<Expander>(tmp);
MessageBox.Show(expander?.Name ?? String.Empty);
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mm8 |