繁体   English   中英

如何在自定义控件Style / DataTemplate中处理事件

[英]How to handle events in a custom control Style / DataTemplate

我有一个自定义控件和一个单独的ResourceDictionary。 如您所见,我已经实现了带有Command的版本,该命令正在运行! 但是我想知道,是否可以直接将此事件注册到后面的代码中? 我需要操纵单击的项目。

代码(已整理)

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;
            }
        }
    }
}

样式(完整)

<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>

是的你可以。

您只需要在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">

然后,添加一个新代码文件,该文件的名称为现有ResourceDictionary的名称,后跟.cs 例如,如果您的ResourceDictionary文件名是HTBoardResources.xaml那么后面代码的文件名就应该是HTBoardResources.xaml.cs

文件后面代码中的类应如下所示:

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

现在,您可以在这个新类中声明Style中任何元素的EventHandler。

(从技术上讲,您不必指定: ResourceDictionary但是如果您这样做,则一眼便可以看到您在ResourceDictionary中。)

您可以在另一个文件中编写样式,然后使用x:Class="HandlerClass"将C#类附加到该x:Class="HandlerClass"

您可以在那里处理所有事件,但请记住,除了当前项目外,它无权访问其他任何内容。

<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">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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