问题描述
我有这样一个自定义控件:
I have a custom control like:
public sealed class BorderEx : Control
{
public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent("ReloadClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx));
public event RoutedEventHandler ReloadClick
{
add { AddHandler(ReloadClickEvent, value); }
remove { RemoveHandler(ReloadClickEvent, value); }
}
void RaiseReloadClickEvent()
{
var newEventArgs = new RoutedEventArgs(ReloadClickEvent);
RaiseEvent(newEventArgs);
}
static BorderEx()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx)));
}
}
和有事件在通用升高的reloadButton点击。 XAML
and there is event rised on reloadButton click at generic.xaml
<ControlTemplate TargetType="{x:Type cbr:BorderEx}">
<ControlTemplate.Triggers>
<Trigger SourceName="reloadButton" Property="IsPressed" Value="True">
<Setter TargetName="reloadButton" Property="Background" Value="Green"/>
<EventSetter Event="ReloadClick" Handler="RaiseReloadClickEvent"></EventSetter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate
不过,我不知道如何提出这个从内部按钮外部事件。最后花几个小时的谷歌搜索,最终什么也没有。上述EventSetter是行不通的。
But I have no idea how to raise this external event from internal button. Spend last few hours googling and ended up with nothing. above EventSetter is not working.
推荐答案
您事件看起来不错,但事实上, EventSetter
不能在触发器中设置。从的:
Your event looks great, but the fact that EventSetter
can not be set in the trigger. Quote from link:
由于使用EventSetter接线事件处理程序是一个编译时的功能,通过IStyleConnector接口管道连接,有一个名为IComponentConnector另一个接口所使用的XAML编译器连线为独立的XAML元素的事件处理程序。
可以做到这一点。确定 EventSetter
外触发,如在早期风格
/ 模板
:
You can do this. Identify EventSetter
outside trigger, such as in the early Style
/ Template
:
<Style TargetType="{x:Type local:BorderEx}">
<EventSetter Event="Button.Click" Handler="ReloadClickEvent" />
...
</Style>
背后
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ReloadClickEvent(object sender, RoutedEventArgs e)
{
RaiseEvent(new DemoEventArgs(BorderEx.ReloadClickEvent, sender));
}
}
public class DemoEventArgs : RoutedEventArgs
{
public DemoEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source)
{
MessageBox.Show("Raise!");
}
}
public sealed class BorderEx : Control
{
public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent("ReloadClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx));
public event RoutedEventHandler ReloadClick
{
add { AddHandler(ReloadClickEvent, value); }
remove { RemoveHandler(ReloadClickEvent, value); }
}
private void RaiseReloadClickEvent()
{
var newEventArgs = new RoutedEventArgs(ReloadClickEvent);
RaiseEvent(newEventArgs);
}
static BorderEx()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx)));
}
}
或者,使用的DependencyProperty
(也可以是的连接的)。例如:
Or alternatively, use the DependencyProperty
(can also be attached). Example:
属性定义:
public static readonly DependencyProperty SampleProperty =
DependencyProperty.RegisterAttached("Sample",
typeof(bool),
typeof(SampleClass),
new UIPropertyMetadata(false, OnSample));
private static void OnSample(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue) == true)
{
// do something...
}
}
$ ; b $ b
调用在XAML
Calling in XAML.
在的EventTrigger
:
<EventTrigger SourceName="MyButton" RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyBox" Storyboard.TargetProperty="(local:SampleClass.Sample)">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>True</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
在<$ C与 DataTrigger
(使用$ C>风格 / 的DataTemplate
/ 等
):
Using with DataTrigger
(in Style
/DataTemplate
/etc
):
<DataTrigger Binding="{Binding ElementName=MyBox, Path=Status), Mode=OneWay}" Value="True">
<Setter Property="(local:SampleClass.Sample)" Value="True" />
</DataTrigger>
在<$ C与触发
(使用$ C>风格):
Using with Trigger
(in Style
):
<Trigger Property="MyCheckBox.IsChecked" Value="True">
<Setter Property="(local:SampleClass.Sample)" Value="True" />
</Trigger>
使用后面的代码:
private void Clear_Click(object sender, RoutedEventArgs e)
{
SampleClass.SetSampleClass(MyBox, true);
}
这篇关于从自定义控件传播活动,形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!