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

时间:2016-10-07 08:12:09

标签: c# wpf

我有一个自定义控件和一个单独的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>

2 个答案:

答案 0 :(得分:2)

是的,你可以。

您只需要在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中。)

答案 1 :(得分:1)

您可以在不同的文件中编写样式,然后使用x:Class="HandlerClass"将C#类附加到其中。

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

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