【问题标题】:How do I apply a style resource to a usercontrol where that style uses triggers to apply templates?如何将样式资源应用于该样式使用触发器应用模板的用户控件?
【发布时间】:2012-10-03 03:44:40
【问题描述】:

如果这是一个简单的问题,我深表歉意,但我一整天都在努力解决这个问题,我似乎无法弄清楚可能相当明显的事情。

我有一个带有自定义依赖属性“Flipped”的用户控件。

在我的资源中,我有一个定义了两个触发器的样式,根据“翻转”是真还是假,将控件的模板设置为两个不同的值。

现在,我使用什么语法将样式应用于我正在创建的这个用户控件?

Style="{StaticResource EventStyle}" 放在UserControl 的标题中是行不通的。

这是我目前所拥有的:

<UserControl x:Class="XDPClient.Controls.EventMarkerControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:Controls="clr-namespace:XDPClient.Controls" 
         mc:Ignorable="d" 
         d:DesignHeight="513" d:DesignWidth="695">

<UserControl.Resources>
    <!-- Draw the user control right side up with this template -->
    <ControlTemplate x:Key="Up" TargetType="{x:Type Controls:EventMarkerControl}">
        <Grid>
            <!-- Outline grid  -->
            <Path Stretch="Fill" Fill="#FFFFFFFF" Stroke="#FF6800FF" StrokeThickness="3" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="4" Name="rect2985" RenderTransformOrigin="0,0">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero"   Figures="M 0,250 L 0,50 L 150,50 L 200,-0.96 L 250,50 L 400,50 L 400,250 L 0,250z" />
                </Path.Data>
            </Path>

            <!-- Placement of the content -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="4*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="1" Margin="3">
                    <ContentPresenter Grid.Row="1" 
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"        
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Grid>

            <Grid.BitmapEffect>
                <DropShadowBitmapEffect />
            </Grid.BitmapEffect>
        </Grid>
    </ControlTemplate>

    <!-- Draw the user control flipped with this template -->
    <ControlTemplate x:Key="Down" TargetType="{x:Type Controls:EventMarkerControl}">
        <Grid>
            <!-- Outline grid  -->
            <Path Stretch="Fill" Fill="#FFFFFFFF" Stroke="#FF6800FF" StrokeThickness="3" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="4" Name="rect2985" RenderTransformOrigin="0,0">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero"   Figures="M 0,250 L 0,50 L 150,50 L 200,-0.96 L 250,50 L 400,50 L 400,250 L 0,250z" />

                </Path.Data>
                <Path.RenderTransform>
                    <ScaleTransform ScaleX="-1" />
                </Path.RenderTransform>
            </Path>

            <!-- Placement of the content -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="4*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="1" Margin="3">
                    <ContentPresenter Grid.Row="1" 
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"        
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Grid>

            <Grid.BitmapEffect>
                <DropShadowBitmapEffect />
            </Grid.BitmapEffect>
        </Grid>
    </ControlTemplate>

    <Style x:Key="EventStyle" TargetType="{x:Type Controls:EventMarkerControl}">
        <Style.Triggers>
            <Trigger Property="Flipped" Value="false">
                <Setter Property="Template" Value="{StaticResource Up}" />
            </Trigger>
            <Trigger Property="Flipped" Value="true">
                <Setter Property="Template" Value="{StaticResource Down}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

【问题讨论】:

  • 把它放在用户控件的主体中会导致 ide 崩溃(我假设是递归)
  • 你能把剩下的 UserControl xaml 代码也显示出来吗?
  • @DJ 那我的整个 UserControl xaml。当然,在名为“Flipped”的代码中定义了一个依赖属性,它是一个布尔值。

标签: .net wpf xaml user-controls wpf-controls


【解决方案1】:

您可以尝试使 UserControl 有一个顶级子项,例如 ContentControl,然后将样式应用于 ContentControl。这样,您就不会得到递归。

编辑:以下来自原始提问者......

这是我按照您的建议做的尝试。出于某种原因,我的 DataTriggers 从不应用模板....不确定我在这里做错了什么。

这是使用包含的 ContentControl 尝试的整个 XAML:

<UserControl x:Class="XDPClient.Controls.EventMarkerControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Controls="clr-namespace:XDPClient.Controls" mc:Ignorable="d" 
         d:DesignHeight="513" d:DesignWidth="695"
         x:Name="uc">

<UserControl.Resources>
    <!-- Draw the user control right side up with this template -->
    <ControlTemplate x:Key="Up" TargetType="{x:Type ContentControl}">
        <Grid>
            <!-- Outline grid  -->
            <Path Stretch="Fill" Fill="#FFFFFFFF" Stroke="#FF6800FF" StrokeThickness="3" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="4" Name="rect2985" RenderTransformOrigin="0,0">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero"   Figures="M 0,250 L 0,50 L 150,50 L 200,-0.96 L 250,50 L 400,50 L 400,250 L 0,250z" />
                </Path.Data>
            </Path>

            <!-- Placement of the content -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="4*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="1" Margin="3">
                    <ContentPresenter Grid.Row="1" 
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"        
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Grid>

            <Grid.BitmapEffect>
                <DropShadowBitmapEffect />
            </Grid.BitmapEffect>
        </Grid>
    </ControlTemplate>

    <!-- Draw the user control flipped with this template -->
    <ControlTemplate x:Key="Down" TargetType="{x:Type ContentControl}">
        <Grid>
            <!-- Outline grid  -->
            <Path Stretch="Fill" Fill="#FFFFFFFF" Stroke="#FF6800FF" StrokeThickness="3" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="4" Name="rect2985" RenderTransformOrigin="0,0">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero"   Figures="M 0,250 L 0,50 L 150,50 L 200,-0.96 L 250,50 L 400,50 L 400,250 L 0,250z" />

                </Path.Data>
                <Path.RenderTransform>
                    <ScaleTransform ScaleX="-1" />
                </Path.RenderTransform>
            </Path>

            <!-- Placement of the content -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="4*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="1" Margin="3">
                    <ContentPresenter Grid.Row="1" 
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"        
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Grid>

            <Grid.BitmapEffect>
                <DropShadowBitmapEffect />
            </Grid.BitmapEffect>
        </Grid>
    </ControlTemplate>


</UserControl.Resources>

<ContentControl x:Name="ContentControl" >
    <!-- Style for the content control -->
    <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
            <Style.Triggers>
                <DataTrigger Value="False" Binding="{Binding ElementName=uc, Path=Flipped}">
                    <Setter Property="ContentControl.Template" Value="{StaticResource Up}" />
                </DataTrigger>
                <DataTrigger Value="True" Binding="{Binding ElementName=uc, Path=Flipped}">
                    <Setter Property="ContentControl.Template" Value="{StaticResource Down}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>
</UserControl>

如果我像这样手动应用样式:

    <ContentControl x:Name="ContentControl" Template="{StaticResource Up}">

看起来不错!只是我的数据触发器没有触发“翻转”的值

【讨论】:

  • 编辑了您的评论以显示您的想法的无效实施。
  • 样式仍然需要进入 UserControl 的样式中。您将使用普通触发器,而不是 DataTrigger,因为 Flipped 属性是在您的 UserControl 上定义的,并且是具有 TargetName="ContentControl" 的 setter,因此它适用于 UserControl 内名为“ContentControl”的控件。
猜你喜欢
  • 2011-09-09
  • 2012-04-10
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多