基于XAML样式的条件基础

时间:2017-01-05 16:14:32

标签: wpf xaml

假设:

Private Sub Painted(sender As Object, e As PaintEventArgs)
    WaterContainer.Anchor = AnchorStyles.Left Or AnchorStyles.Right
    WaterContainer.Height = Me.Height

    'Declare a variable indicating whether RightToLeft is set or not.
    Dim RightToLeft As Boolean = (Me.RightToLeft = Windows.Forms.RightToLeft.Yes)

    'Set the watermark's client width according to the text's width.
    Dim MeasuredTextSize As Size = TextRenderer.MeasureText(WaterText, WaterFont, Me.Size, If(RightToLeft, TextFormatFlags.RightToLeft, TextFormatFlags.Default))
    WaterContainer.ClientSize = New Size(MeasuredTextSize.Width, WaterContainer.ClientSize.Height)

    'Determine the watermark's alignment.
    If (Me.TextAlign = HorizontalAlignment.Left AndAlso RightToLeft = False) OrElse (Me.TextAlign = HorizontalAlignment.Right AndAlso RightToLeft = True) Then
        WaterContainer.Location = New Point(2, 0) 'Left alignment, or right alignment with RightToLeft set.

    ElseIf Me.TextAlign = HorizontalAlignment.Center Then
        WaterContainer.Location = New Point((Me.Width / 2) - (WaterContainer.Width / 2), 0) 'Center alignment.

    ElseIf (Me.TextAlign = HorizontalAlignment.Right AndAlso RightToLeft = False) OrElse (Me.TextAlign = HorizontalAlignment.Left AndAlso RightToLeft = True) Then
        WaterContainer.Location = New Point((Me.Width - WaterContainer.Width) - 2, 0) 'Right alignment, or left alignment with RightToLeft set.

    End If

    WaterBrush = New SolidBrush(WaterColor)

    Dim Graphic As Graphics = e.Graphics
    Graphic.DrawString(WaterText, WaterFont, WaterBrush, New PointF(-2.0! + If(RightToLeft, MeasuredTextSize.Width, 0), 1.0!), New StringFormat(CType(If(RightToLeft, StringFormatFlags.DirectionRightToLeft, 0), Integer)))
End Sub

<Style x:Key="ThirdLevelGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource MetroGroupBox}"> <Setter Property="Background" Value="{DynamicResource AccentColorBrush3}" /> </Style> <Style x:Key="SecondLevelGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource MetroGroupBox}"> <Setter Property="Background" Value="{DynamicResource AccentColorBrush2}" /> </Style> <Style TargetType="GroupBox" x:Key="WidgetControlTemplateStyle" BasedOn="{StaticResource ThirdLevelGroupBoxStyle}"> <Style.Triggers> <DataTrigger Binding="{Binding CanExecuteClickCommand}" Value="True"> <!-- TODO: SecondLevelGroupBoxStyle --> <Setter Property="Background" Value="{DynamicResource AccentColorBrush2}" /> </DataTrigger> </Style.Triggers> </Style> <ControlTemplate TargetType="ContentControl" x:Key="WidgetControlTemplate"> <GroupBox ... Style="{StaticResource WidgetControlTemplateStyle}"> <ContentPresenter /> </GroupBox> </ControlTemplate> 的格式为ControlTemplate。我希望在WidgetControlTemplateBasedOn上有条件地设置WidgetControlTemplate样式ThirdLevelGroupBoxStyle,以避免XAML重复。有没有办法实现这个目标?

如果我不能这样做,我必须复制SecondLevelGroupBoxStyle的定义。

1 个答案:

答案 0 :(得分:0)

  

有没有办法实现这个目标?

不,不在XAML中。必须在设计时知道基于样式的基本样式。

  

如果我不能这样做,我必须复制SecondLevelGroupBoxStyle的定义。

由于您有两种基于MetroGroupBox的不同样式,因此基于其中任何一种样式的每种样式将始终是一种单独的样式,因为您不能将单一样式基于多种样式。

您可以使用触发器将GroupBox的Background属性设置为AccentColorBrush3或AccentColorBrush2,而不是尝试从其他样式继承此属性。看起来你已经在你的WidgetControlTemplateStyle风格中这样做了。是的,如果ThirdLevelGroupBoxStyle / SecondLevelGroupBoxStyle设置了多个属性,您还必须在WidgetControlTemplateStyle中设置所有这些属性。

我担心除非你将两种风格合并为一种,或者以某种方式以编程方式定义它们,否则无法解决这个问题。