Share via

WPF Powershell Adding event to Control in a template or style

Walter Beach 1 Reputation point
2021-02-25T20:02:14.453+00:00

Hello, How do I add an event to a System.Windows.Control contained within a Style or Template? Because it is in a Template or Style I can't do a FindName. Example: I am using a groupstyle in a Datagrid in order to add a checkbox with the content being set to the group name. When I check or uncheck the box I want all the rows within that group to also uncheck and check. Bonus would be to set the value to be able to do the reverse where on row selection change it would go to true, false, or null based on selected values. Thank you, Walter

Developer technologies | Windows Presentation Foundation
0 comments No comments

2 answers

Sort by: Most helpful
  1. Mouad Cherkaoui 6 Reputation points
    2021-02-28T22:35:20.897+00:00

    alternatively, and in a more simple way you can use defined styles in your window:

    <Window.Resources>
        <Style x:Key="checkbox_evt" TargetType="CheckBox">
            <EventSetter Event="Checked" Handler="CheckBox_Checked"></EventSetter>
        </Style>
        <Style x:Key="griditem_evt" TargetType="DataGridRow">
            <EventSetter Event="Selected" Handler="DataGridRow_Selected"></EventSetter>
        </Style>
    </Window.Resources>
    

    and reference them in your controls:

        <CheckBox Style="{Binding Source={StaticResource checkbox_evt}}"></CheckBox>
        <DataGrid RowStyle="{Binding Source={StaticResource griditem_evt}}"></DataGrid>
    

    Was this answer helpful?

    0 comments No comments

  2. Mouad Cherkaoui 6 Reputation points
    2021-02-28T22:17:57.89+00:00

    Hi,
    in the case of Checkbox control you can use the Command property in your xaml template to using a binding to your command, for some controls where there is no way to capture events with commands binding you will need to add them using attached properties like when using "System.Windows.Interactivity"
    lets take a look to the first example:

    <CheckBox Content="CheckBox"
              Command="{Binding WhenCheckedCommand}"
              CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
    

    here you bind the changing state of the checkbox to the WhenCheckedCommand which take the IsChecked state from self "the checkbox" as parameter.

    for the second case, you should install System.Windows.Interactivity library and have its dll in the execution folder, in your xaml add a namespace declaration:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    

    also you will need mvvmlight which will provide you an EventToCommand converter:

    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
    

    and in you can use them in your templates like this:

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Checked">
            <cmd:EventToCommand Command="{Binding Path=WhenCheckedCommand,Mode=OneWay}" CommandParameter="{Binding IsChecked, ElementName=YourCheckBox}"></cmd:EventToCommand>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    

    Note: some controls do not support adding behaviors, you can then use your own attached properties, here is a great article demonstrating how to bind a command to a specific event:
    http://www.mobilemotion.eu/?p=1822

    if you are not using MVVM pattern the simple way is to use event handlers adders methods, let say we need to add an event handler to SelectionChangedEvent for a listbox you should do something like this:

    $listBox.add_SelectionChanged({
        $textBox.Text = $listBox.SelectedItem
    })
    

    the example is from this article:
    https://devblogs.microsoft.com/powershell/wpf-powershell-part-3-handling-events/

    also share a part of your implementation that we can give a specific solution for it.

    Hope it helps!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.