如何从样式触发器设置依赖项属性

时间:2013-08-02 11:52:41

标签: c# wpf

我正在使用以下样式集创建自定义wpf控件。在后面的代码中,我有一个名为selected的依赖属性。当操作OnMouseLeftButtonDown触发器时,如何将此属性设置为true?

<Style TargetType="{x:Type local:myControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:myControl}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="OnMouseLeftButtonDown">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF9B1111"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="textBlock">
                            <BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown}"/>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="textBlock2">
                            <BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown}"/>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Border Name="border" BorderBrush="#FFDEDEDE" BorderThickness="1" Margin="1" >
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="2,1,2,3" Text="{TemplateBinding ElementName}" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF7C8CF1"  FontSize="10" FontWeight="Bold"></TextBlock>
                                <TextBlock Name="textBlock2" HorizontalAlignment="Left" Margin="2,1,2,3" Text="{TemplateBinding ElementText}" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF75757A" FontSize="10" FontWeight="Bold"></TextBlock>
                            </StackPanel>
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

2 个答案:

答案 0 :(得分:0)

Storyboard OnMouseLeftButtonDown 中,您可以添加ObjectAnimationUsingKeyFrames。样品:

<Storyboard x:Key="OnMouseLeftButtonDown">
    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
        <EasingColorKeyFrame KeyTime="0" Value="#FF9B1111"/>
    </ColorAnimationUsingKeyFrames>

    <!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" -->
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="YouTarget" Storyboard.TargetProperty="NameOfYourProperty">
        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <sys:Boolean>True</sys:Boolean>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

对于附加的依赖项属性必须这样写:

Storyboard.TargetProperty="(local:YourDependecyClass.IsSampleMyProperty)"

答案 1 :(得分:0)

我最后这样做了。我将控件的名称更改为PART_ElementName和PART_ElementText我在OnApplyTemplate()方法中执行了以下操作

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _elementName = Template.FindName("PART_ElementName", this) as TextBlock;
            _elementText = Template.FindName("PART_ElementText", this) as TextBlock;

            if (_elementName != null) _elementName.MouseLeftButtonDown += (sender, args) => SelectControl();
            if (_elementText != null) _elementText.MouseLeftButtonDown += (sender, args) => SelectControl();
        }

然后我在SelectControl()方法中进行更改