[英]How to handle events in a custom control Style / DataTemplate
I have a custom control and a seperate ResourceDictionary. 我有一个自定义控件和一个单独的ResourceDictionary。 As you can see, I have already implemented a version with a Command which is working! 如您所见,我已经实现了带有Command的版本,该命令正在运行! 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. 我需要操纵单击的项目。
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>
Yes, you can. 是的你可以。
You just have to declare the class in the definition of the ResourceDictionary: 您只需要在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
. 然后,添加一个新代码文件,该文件的名称为现有ResourceDictionary的名称,后跟.cs
。 Eg if your ResourceDictionary file name is HTBoardResources.xaml
then the file name for the code behind needs to be HTBoardResources.xaml.cs
. 例如,如果您的ResourceDictionary文件名是HTBoardResources.xaml
那么后面代码的文件名就应该是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. 现在,您可以在这个新类中声明Style中任何元素的EventHandler。
(Technically you don't have to specify : ResourceDictionary
but if you do then you see at one glance that you are in a ResourceDictionary.) (从技术上讲,您不必指定: ResourceDictionary
但是如果您这样做,则一眼便可以看到您在ResourceDictionary中。)
You can write your style in a different file, then attach a C# class to it with x:Class="HandlerClass"
. 您可以在另一个文件中编写样式,然后使用x:Class="HandlerClass"
将C#类附加到该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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.