[英]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.