代码之家  ›  专栏  ›  技术社区  ›  Quark Soup

如何在控件模板的元素上执行命令?

  •  0
  • Quark Soup  · 技术社区  · 5 年前

    我想构建一个使用自定义画笔的自定义控件。这个想法是,当我在自定义控件上执行命令时,我希望它能过滤到模板中的一个组件。我的第一次传球看起来像这样:

    <Style TargetType="trrfc:QuantityControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="trrfc:QuantityControl">
                    <Border>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <ContentPresenter.Foreground>
                                <trrfc:AnimatedColorBrush StartColor="Red"
                                                          Animate="{Binding Mode=TwoWay, RelativeSource={RelativeSource  TemplatedParent}, Path=Animate}"
                                                          Duration="00:00:05"/>
                            </ContentPresenter.Foreground>
                        </ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    也就是说,我想在 动画彩色画笔 这是正确的做法吗?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Nico Zhu    5 年前

    如何在控件的元素上执行命令?

    对于该场景,您可以使用 InvokeCommandAction 在哪里 XamlBehaviors 图书馆要走近。

    例如,如果您想在按钮单击时调用命令。你可以用 EventTriggerBehavior 检测点击事件,然后触发 调用命令操作 如下所示。

    <Button x:Name="MyButton">
        <Interactivity:Interaction.Behaviors>
            <Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=MyButton}">
                <Interactions:InvokeCommandAction Command="{Binding UpdateCountCommand}"/>
            </Interactions:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </Button>