Tamar Cohen
Tamar Cohen

Reputation: 1292

WPF Which control invoked the event that was defined in a custom style

I have written a custom style (including template) for the slider control in WPF, which has 2 buttons inside ('MinusBtn" and "PlusBtn").

Now, I want to use the same slider custom style for 4 different sliders, and I want the buttons to do the same thing when I press it on everyone of the 4 sliders ("MinusBtn_Clicked" event and "PlusBtn_Clisked" event). But I need to know which slider invoked the event.

Since the buttons configured in the generic style, I cannot know it by their parent. How can I fix this? Can I add parameters to the delegate of this event?

Here is my custom style:

 <Style x:Key="MyCustomStyleForSlider" TargetType="{x:Type Slider}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Slider}">  
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="10.5"/>
                            <RowDefinition Height="Auto" MinHeight="{TemplateBinding Slider.MinHeight}"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="47*"/>
                            <ColumnDefinition Width="331*"/>
                            <ColumnDefinition Width="47*"/>
                        </Grid.ColumnDefinitions>

                        <TickBar Name="BottomTick" SnapsToDevicePixels="True" Grid.Row="2" Fill="{TemplateBinding Foreground}"
                                Placement="Bottom"
                                Height="4"
                                Visibility="Collapsed" />

                        <Grid x:Name="JustTrack" Height="20" Width="335" Grid.RowSpan="1" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1">
                            <Track Grid.Row="1" Name="PART_Track">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton Style="{StaticResource DecreaseSliderButtonStyle}"  />
                                </Track.DecreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb Style="{StaticResource SliderThumbStyle}" />
                                </Track.Thumb>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton Style="{StaticResource SliderButtonStyle}" />
                                </Track.IncreaseRepeatButton>
                            </Track>
                        </Grid>
                    <Grid x:Name="Minus" Grid.Column="0" Grid.RowSpan="3" Width="47" Height="40">
                            <Button x:Name="MinusBtn" Click="MinusBtn_Click" >
                                <Button.Template>
                                    <ControlTemplate>
                                        <Ellipse Height="25" Width="25" Fill="Transparent"></Ellipse>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                        <Grid x:Name="Plus" Grid.Column="2" Grid.RowSpan="3" Width="47" Height="40">
                            <Button x:Name="PlusBtn" Click="PlusBtn_Click">
                                <Button.Template>
                                    <ControlTemplate>
                                        <Ellipse Height="25" Width="25" Fill="Transparent"></Ellipse>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="TickPlacement" Value="BottomRight">
                            <Setter TargetName="BottomTick" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Upvotes: 0

Views: 665

Answers (1)

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

If you want to increase or decrease the value of the slider with the buttons. Use the Command property of the buttons:

<Grid x:Name="Minus" Grid.Column="0" Grid.RowSpan="3" Width="47" Height="40">
    <Button x:Name="MinusBtn" Command="Slider.DecreaseSmall">
        <Button.Template>
            <ControlTemplate>
                <Ellipse Height="25" Width="25" Fill="Transparent"></Ellipse>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>
<Grid x:Name="Plus" Grid.Column="2" Grid.RowSpan="3" Width="47" Height="40">
    <Button x:Name="PlusBtn" Command="Slider.IncreaseSmall">
        <Button.Template>
            <ControlTemplate>
                <Ellipse Height="25" Width="25" Fill="Transparent"></Ellipse>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

Or if you really want the slider in your event handler, you can do:

private void Button_Click(object sender, RoutedEventArgs e)
{
    FrameworkElement frameworkElement = (FrameworkElement)sender;
    Slider slider = (Slider)frameworkElement.TemplatedParent;
}

Upvotes: 3

Related Questions