0

使用知道如何“看起来”禁用的自定义内容创建 Silverlight 按钮的最简单方法是什么?即如果你设置IsEnabled="False"它会看起来是灰色的。

自定义内容将非常简单,文本和图像。

我之前在 WPF 应用程序中通过将 Content 设置为包含 TextBlock 和 Image 的 StackPanel 很容易地做到了这一点。然后,我在图像上实现了一个样式触发器,以在未启用时将其更改为灰色版本。文本自己改变了颜色。

据我所知,当 Silverlight 中禁用该按钮时,自定义内容会完全消失。

任何帮助表示赞赏。

干杯,安德烈。

4

2 回答 2

3

假设您没有更改 Button 的模板,Button 的默认控件模板使用 VisualStateManager 以 50% 的透明度覆盖按钮中的任何内容的白色矩形。这应该使内容看起来“褪色”。

如果您已替换模板,则需要复制此行为。Silverlight 没有样式触发器,因此您需要使用 VisualStateManager。你有表达混合吗?如果是这样,您可以通过将按钮拖到设计器上来查看默认控件模板,右键单击 -> 编辑模板 -> 编辑副本。

编辑

我已经包含了 Blend 提取的 Button 的默认控件模板。记下 VisualStateManager 中的禁用状态。

<ControlTemplate TargetType="Button">
  <Grid>
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="MouseOver">
          <Storyboard>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity">
              <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Pressed">
          <Storyboard>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#FF6DBDD1"/>
            </ColorAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity">
              <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#D8FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#8CFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
              <SplineColorKeyFrame KeyTime="0" Value="#3FFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Disabled">
          <Storyboard>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
              <SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
            </DoubleAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
      <VisualStateGroup x:Name="FocusStates">
        <VisualState x:Name="Focused">
          <Storyboard>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity">
              <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Unfocused"/>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Border
      x:Name="Background"
      Background="White"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}"
      CornerRadius="3">
      <Grid Margin="1" Background="{TemplateBinding Background}">
        <Border x:Name="BackgroundAnimation" Opacity="0" Background="#FF448DCA"/>
        <Rectangle x:Name="BackgroundGradient">
          <Rectangle.Fill>
            <LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
              <GradientStop Color="#FFFFFFFF" Offset="0"/>
              <GradientStop Color="#F9FFFFFF" Offset="0.375"/>
              <GradientStop Color="#E5FFFFFF" Offset="0.625"/>
              <GradientStop Color="#C6FFFFFF" Offset="1"/>
            </LinearGradientBrush>
          </Rectangle.Fill>
        </Rectangle>
      </Grid>
    </Border>
    <ContentPresenter
      x:Name="contentPresenter"
      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
      Margin="{TemplateBinding Padding}"
      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
      Content="{TemplateBinding Content}"
      ContentTemplate="{TemplateBinding ContentTemplate}"/>
    <Rectangle
      x:Name="DisabledVisualElement"
      Fill="#FFFFFFFF"
      RadiusX="3"
      RadiusY="3"
      IsHitTestVisible="false"
      Opacity="0"/>
    <Rectangle
      x:Name="FocusVisualElement"
      Stroke="#FF6DBDD1"
      StrokeThickness="1"
      RadiusX="2"
      RadiusY="2"
      Margin="1"
      IsHitTestVisible="false"
      Opacity="0"/>
  </Grid>
</ControlTemplate>
于 2010-04-07T04:44:21.393 回答
1

稍微降低按钮的 Opacity 属性可能有助于使其看起来像已禁用。

于 2010-04-07T06:05:26.690 回答