WPF样式/控件模板重用

时间:2010-06-19 08:10:58

标签: c# .net wpf xaml code-reuse

我是WPF的新手,我想知道如何重用一些烦人的xaml,我必须避免重复。

<Button Cursor="Hand" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="MyButton" Style="{StaticResource ButtonTemplate}" Width="286" Content="hi!" Focusable="False" IsTabStop="False"/>
<Button Cursor="Hand" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="MyButton2" Style="{StaticResource ButtonTemplate}" Width="286" Content="hi 2!" Focusable="False" IsTabStop="False"/>

我真的很喜欢使用这样的模板:

<Style TargetType="{x:Type Button}" x:Key="ButtonTemplate">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="btGrid">
                            <Path Cursor="Hand" HorizontalAlignment="Left" Stretch="Fill" Stroke="{x:Null}" Opacity="0" x:Name="path"/>
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" Visibility="Hidden" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown">
                                <EventTrigger.Actions>
                                    <BeginStoryboard>
                                        <Storyboard SlipBehavior="Slip" BeginTime="00:00:00">
                                            <MediaTimeline Source="{Binding StringFormat={}, Path=Name}" Storyboard.TargetName="{Binding StringFormat={}_wma, Path=Name}"/>
                                                    <ObjectAnimationUsingKeyFrames    Storyboard.TargetName="{Binding StringFormat=key{}, Path=Name}" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>
                        Visible
                    </Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonUp">
                                <EventTrigger.Actions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="{Binding StringFormat=key{}, Path=Name}" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>
                        Hidden
                    </Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>
                            <Trigger Property="IsFocused" Value="True"/>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True"/>
                            <Trigger Property="IsPressed" Value="True"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我希望{Binding StringFormat = {},Path = Name}指向按钮的名称,例如“MyButton”,“MyButton2”等。

当我运行此代码时,我收到错误“无法冻结此Storyboard时间轴树以供跨线程使用”。 :/我明白这是因为我在故事板中使用绑定,对吗?我不知道如何做到这一点。

此外,我还希望将图像的ToggleVisibility设置为模板,它接受一次“可见”和一次“隐藏”值。 提前谢谢!

2 个答案:

答案 0 :(得分:1)

您也可以随时在样式中定义除Template之外的属性。

    <Style TargetType="{x:Type Button}"
           x:Key="ButtonTemplate">
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Margin" Value="0,0,0,0" />
        <Setter Property="Width" Value="286" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    ...
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这使您的代码看起来像

    <Button x:Name="MyButton" Style="{StaticResource ButtonTemplate}" Content="hi!" />
    <Button x:Name="MyButton2" Style="{StaticResource ButtonTemplate}" Content="hi 2!" />

答案 1 :(得分:0)

是的,将目标类型的样式创建为按钮就可以了。

Tip:在代码的资源部分下编写所有样式信息(如边框,背景,模板等)并将其应用于控件总是一个好习惯。它会提供良好的可读性。

HTH:)