简体   繁体   中英

How to handle events in a custom control Style / DataTemplate

I have a custom control and a seperate ResourceDictionary. As you can see, I have already implemented a version with a Command which is working! But I want to know, if it is possible that I can register this event directly to my code behind? I need to manipulate the clicked item.

Code (Trimmed)

public class HTBoard : Control, INotifyPropertyChanged
{

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        if (this.Template != null)
        {
            _ListBox = GetTemplateChild("ContentListbox") as ListBox;
            _DragSelectionCanvas = GetTemplateChild("DragSelectionCanvas") as Canvas;
            _DragSelectionBorder = GetTemplateChild("DragSelectionBorder") as Border;
            //_Item = GetTemplateChild("Item") as ContentPresenter;

            if (_ListBox == null || _DragSelectionCanvas == null || _DragSelectionBorder == null)
            {

            }
            else
            {
                //_Item.MouseDown += Item_MouseDown;
                //_Item.MouseUp += Item_MouseUp;
                //_Item.MouseMove += Item_MouseMove;

                this.MouseDown += HTBoard_MouseDown;
                this.MouseUp += HTBoard_MouseUp;
                this.MouseMove += HTBoard_MouseMove;
            }
        }
    }
}

Style (Full)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:HTFramework="clr-namespace:HTFramework">

<Style TargetType="{x:Type HTFramework:HTBoard}">
    <Style.Resources>
        <DataTemplate DataType="{x:Type HTFramework:HTBoardItem}">
            <Grid 
                Background="#FFD62626" 
                UseLayoutRounding="True"
                Margin="0,2,2,2">
                <ContentPresenter
                    x:Name="Item"
                    Content="{Binding FrameworkElement}"
                    Width="{Binding FrameworkElement.Width}"
                    Height="{Binding FrameworkElement.Height}" 
                    UseLayoutRounding="True">
                    <ContentPresenter.InputBindings>
                        <MouseBinding 
                            Gesture="LeftClick" 
                            Command="{Binding RelativeSource={RelativeSource AncestorType=HTFramework:HTBoard}, Path=ItemClickCommand}" ></MouseBinding>
                    </ContentPresenter.InputBindings>
                    <ContentPresenter.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform Angle="{Binding Rotation}"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </ContentPresenter.RenderTransform>
                </ContentPresenter>
            </Grid>
        </DataTemplate>
    </Style.Resources>
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type HTFramework:HTBoard}">
                    <Grid Background="Transparent">
                        <ListBox
                            x:Name="ContentListbox"
                            ItemsSource="{Binding ItemSource, RelativeSource={RelativeSource TemplatedParent}}"
                            SelectionMode="Extended"
                            Background="{TemplateBinding Background}"
                            Width="{Binding Path=Width, RelativeSource={RelativeSource TemplatedParent}}"
                            Height="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}}">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas></Canvas>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="{x:Type ListBoxItem}">
                                    <Setter Property="Canvas.Left" Value="{Binding X}"></Setter>
                                    <Setter Property="Canvas.Top" Value="{Binding Y}"></Setter>
                                </Style>
                            </ListBox.ItemContainerStyle>
                        </ListBox>
                        <Canvas
                            x:Name="DragSelectionCanvas"
                            Visibility="Collapsed">
                            <Border
                                x:Name="DragSelectionBorder"
                                BorderBrush="Red"
                                BorderThickness="1"
                                Background="LightBlue"
                                CornerRadius="1"
                                Opacity="0.5"></Border>
                        </Canvas>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

Yes, you can.

You just have to declare the class in the definition of the ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:HTFramework="clr-namespace:HTFramework"
    x:Class="HTFramework.HTBoardResources">

Then you add a new code file which has the name of your existing ResourceDictionary followed by .cs . Eg if your ResourceDictionary file name is HTBoardResources.xaml then the file name for the code behind needs to be HTBoardResources.xaml.cs .

The class in the code behind file should look like this:

namespace HTFramework
{
    public partial class HTBoardResources : ResourceDictionary
    {
    }
}

You can now declare the EventHandler of any element in your Style in this new class.

(Technically you don't have to specify : ResourceDictionary but if you do then you see at one glance that you are in a ResourceDictionary.)

You can write your style in a different file, then attach a C# class to it with x:Class="HandlerClass" .

You can handle all events there, but keep in mind it does not have access to anything but the current item.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:HTFramework="clr-namespace:HTFramework"
                x:Class="HandlerClass">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM