将XAML行为附加到相同类型的所有控件

dub*_*lee 2 wpf xaml attachedbehaviors

我有一个InvokeCommandAction与我喜欢的GotFocus事情相关的TextBox事情:

<TextBox Grid.Row="0"
         Grid.Column="1"
         Width="40"
         HorizontalAlignment="Right">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="GotFocus">
            <i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="Enter data [message to be displayed]" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

它的工作方式很好,但是我有几十个TextBox这样的设置.而不是重复代码(正如我目前为每个代码所做的那样),我希望将该触发器附加到所有类型的控件{x:Type TextBox}.

通常,我会在该Resources部分设置属性,如下所示:

<UserControl.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Padding" Value="5,0,0,0" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不适用于Triggers:

附加属性"触发器"只能应用于从"DependencyObject"派生的类型.

理想情况下,我想做这样的事情:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBox}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding Tag}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

然后我只需要设置Tag每个属性TextBox来指定要显示的消息.我是在正确的轨道上吗?我是否需要将其更改为使用ControlTemplate或类似的东西?

编辑

我在这里看到了类似的问题:ResourceDictionary WPF中的Style中的交互触发器

在阅读了该问题的答案后,我尝试了以下方法:

<UserControl.Resources>
    <TextBox x:Key="TextBoxWithTag">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

然后像这样分配给一个控件:

<ContentControl Grid.Row="0"
                Grid.Column="1"
                Width="40"
                HorizontalAlignment="Right"
                Content="{StaticResource TextBoxWithTag}"
                Tag="test tag" />
Run Code Online (Sandbox Code Playgroud)

这也不起作用,仍抱怨:

附加属性"触发器"只能应用于从"DependencyObject"派生的类型.

编辑2

这是GotFocusCommand信息.它设置了string一个TextBlock绑定它的值.

这是我的ViewModel:

private ICommand _gotFocusCommand;
public ICommand GotFocusCommand
{
    get
    {
        if (_gotFocusCommand == null)
        {
            _gotFocusCommand = new DelegateCommand<string>(TextBoxGotFocus);
        }
        return _gotFocusCommand;
    }
}

private void TextBoxGotFocus(string infoText)
{
    CardInfoText = infoText;
}
Run Code Online (Sandbox Code Playgroud)

那么XAML:

<TextBlock Grid.Row="2"
           Grid.Column="0"
           Grid.ColumnSpan="2"
           HorizontalAlignment="Center"
           Text="{Binding CardInfoText}" />
Run Code Online (Sandbox Code Playgroud)

Evk*_*Evk 7

有几种方法可以做你想要的.一个例子:

public static class UIBehaviors {
    public static readonly DependencyProperty AttachedTriggersProperty = DependencyProperty.RegisterAttached(
        "AttachedTriggers", typeof (EventTriggerCollection), typeof (UIBehaviors), new PropertyMetadata(null, OnAttachedTriggersChanged));

    private static void OnAttachedTriggersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var triggers = Interaction.GetTriggers(d);
        if (e.OldValue != null) {
            foreach (var trigger in (EventTriggerCollection) e.OldValue) {
                triggers.Remove(trigger);
            }
        }
        if (e.NewValue != null) {
            foreach (var trigger in (EventTriggerCollection) e.NewValue) {
                triggers.Add(trigger);
            }
        }
    }

    public static void SetAttachedTriggers(DependencyObject element, EventTriggerCollection value) {
        element.SetValue(AttachedTriggersProperty, value);
    }

    public static EventTriggerCollection GetAttachedTriggers(DependencyObject element) {
        return (EventTriggerCollection) element.GetValue(AttachedTriggersProperty);            
    }
}

public class EventTriggerCollection : Collection<EventTrigger> {

}
Run Code Online (Sandbox Code Playgroud)

这里我们声明附加属性,它接受一组EventTrigger(来自Interactivity程序集).设置此属性后,我们只需附加所有这些触发器,例如i:Interaction.Triggers即可.然后像这样使用它:

<Window.Resources>
    <local:EventTriggerCollection x:Shared="False" x:Key="textBoxTriggers">
        <i:EventTrigger EventName="GotFocus">
            <i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TextBox}, Path=Tag}" />
        </i:EventTrigger>
    </local:EventTriggerCollection>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="local:UIBehaviors.AttachedTriggers" Value="{StaticResource textBoxTriggers}"/>
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

请注意我如何绑定到TextBox.Tag属性.您不能像在问题中那样绑定它,因为您的数据上下文将是视图模型(使用GotFocusCommand).还要注意触发器集合如何作为资源字典中的单独元素移动,并为其设置x:Shared ="false".这将导致每次访问此属性时都会创建新的触发器集,因此每个TextBox都有自己的触发器集.

那么任何

<TextBox Text="Test" Tag="test message" />
Run Code Online (Sandbox Code Playgroud)

将在TextBox的数据上下文中调用GotFocusCommand,并将"test message"作为参数.