0

如何将以下 XAML 代码集成到按钮背景中。我已经尝试了很多方法,但我很难让它发挥作用。PS 我是 XAML 的新手。

    <Viewbox Width="55.247" Height="55.247"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Width="55.247" Height="55.247">

  <Canvas>

  <!-- button/circle -->
  <Path StrokeThickness="1.0" Stroke="#ff335284" StrokeMiterLimit="1.0" Data="F1 M 54.747,27.624 C 54.747,42.603 42.603,54.747 27.624,54.747 C 12.644,54.747 0.500,42.603 0.500,27.624 C 0.500,12.644 12.644,0.500 27.624,0.500 C 42.603,0.500 54.747,12.644 54.747,27.624 Z">
    <Path.Fill>
      <RadialGradientBrush MappingMode="Absolute" GradientOrigin="392.717,-251.469" Center="392.717,-251.469" RadiusX="27.123" RadiusY="27.123">
        <RadialGradientBrush.GradientStops>
          <GradientStop Offset="0.00" Color="#ff00aae4"/>
          <GradientStop Offset="1.00" Color="#ff2463a7"/>
        </RadialGradientBrush.GradientStops>
        <RadialGradientBrush.Transform>
          <MatrixTransform Matrix="1.000,0.000,-0.000,-1.000,-365.093,-223.845" />
        </RadialGradientBrush.Transform>
      </RadialGradientBrush>
    </Path.Fill>
  </Path>

  </Canvas>
</Canvas>
</Viewbox>
4

2 回答 2

3

以下示例将标准按钮模板替换为您的设计并添加内容演示器,以便您仍然可以设置按钮的内容。

<Button Content="OK" Width="75" Height="75">
    <Button.Template>
        <ControlTemplate TargetType="Button">

            <Grid>
                <Path StrokeThickness="1.0" Stroke="#ff335284" StrokeMiterLimit="1.0" Data="F1 M 54.747,27.624 C 54.747,42.603 42.603,54.747 27.624,54.747 C 12.644,54.747 0.500,42.603 0.500,27.624 C 0.500,12.644 12.644,0.500 27.624,0.500 C 42.603,0.500 54.747,12.644 54.747,27.624 Z">
                  <Path.Fill>
                      <RadialGradientBrush MappingMode="Absolute" GradientOrigin="392.717,-251.469" Center="392.717,-251.469" RadiusX="27.123" RadiusY="27.123">
                          <RadialGradientBrush.GradientStops>
                              <GradientStop Offset="0.00" Color="#ff00aae4"/>
                              <GradientStop Offset="1.00" Color="#ff2463a7"/>
                          </RadialGradientBrush.GradientStops>
                          <RadialGradientBrush.Transform>
                              <MatrixTransform Matrix="1.000,0.000,-0.000,-1.000,-365.093,-223.845" />
                          </RadialGradientBrush.Transform>
                      </RadialGradientBrush>
                 </Path.Fill>
             </Path>
             <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>

    </ControlTemplate>
</Button.Template>

于 2012-08-15T04:41:24.317 回答
1

那么你有多种选择来实现这一点。如果你真的只是想把你的路径放在一个按钮中,你可以用最小的努力做到这一点;

<Button Width="75" Height="75">
    <!-- Embed our elements in a grid since Content can be set only once -->
    <Grid>
        <!-- Your Path -->
        <Path StrokeThickness="1.0" Stroke="#ff335284" StrokeMiterLimit="1.0" Data="F1 M 54.747,27.624 C 54.747,42.603 42.603,54.747 27.624,54.747 C 12.644,54.747 0.500,42.603 0.500,27.624 C 0.500,12.644 12.644,0.500 27.624,0.500 C 42.603,0.500 54.747,12.644 54.747,27.624 Z">
          <Path.Fill>
            <RadialGradientBrush MappingMode="Absolute" GradientOrigin="392.717,-251.469" Center="392.717,-251.469" RadiusX="27.123" RadiusY="27.123">
              <RadialGradientBrush.GradientStops>
                <GradientStop Offset="0.00" Color="#ff00aae4"/>
                <GradientStop Offset="1.00" Color="#ff2463a7"/>
              </RadialGradientBrush.GradientStops>
              <RadialGradientBrush.Transform>
                <MatrixTransform Matrix="1.000,0.000,-0.000,-1.000,-365.093,-223.845" />
              </RadialGradientBrush.Transform>
            </RadialGradientBrush>
          </Path.Fill>
        </Path>
        <!-- Your Button Text -->
        <TextBlock Text="Button" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Button>

如果您需要获得更多技术/更清洁,您只需将其直接放入控制模板中。这是关于WPF 按钮控件模板的文档

希望这会有所帮助,幸运的是,网上也有很多自定义 WPF 按钮的教程。如果您想详细说明您想要什么,例如您是特别为 WPF 还是 SL 拍摄(VisualStateManager 或 Style Triggers 等差异),人们可能会更有用。:)

于 2012-08-15T04:30:22.343 回答