【问题标题】:Canvas as Image in Button WPF画布作为按钮 WPF 中的图像
【发布时间】:2014-11-26 13:53:15
【问题描述】:

我正在将我们项目中的所有图像移至ResourceDictionary 以供所有项目使用。使用 Syncfusion Metro Studio 时,我能够获取图像的 XAML 源。

<Viewbox x:Shared="false" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Grid>
    <Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
    <Path Data="M0,4.1309996L20.362437,4.1309996C20.038338,4.8125897,19.782439,5.5301299,19.594339,6.2769008L2.1451931,6.2769008 2.1451931,23.445208 30.042807,23.445208 30.042807,20.78887C30.342306,20.81167 30.642406,20.834471 30.949104,20.834471 31.367603,20.834471 31.781102,20.81027 32.188001,20.76737L32.188001,25.590239 20.922435,25.590239 20.922435,27.736771 23.605427,27.736771 23.605427,29.882 8.5839529,29.882 8.5839529,27.736771 11.265565,27.736771 11.265565,25.590239 0,25.590239z M29.967411,3.9921243L29.967411,8.1359167 25.823251,8.1359167 25.823251,10.100034 29.967411,10.100034 29.967411,14.243865 31.931586,14.243865 31.931586,10.100034 36.075645,10.100034 36.075645,8.1359167 31.931586,8.1359167 31.931586,3.9921243z M30.948448,0C35.985142,1.0841802E-08 40.067997,4.0825729 40.067997,9.1171007 40.067997,14.153367 35.985142,18.236 30.948448,18.236 25.912456,18.236 21.830998,14.153367 21.830998,9.1171007 21.830998,4.0825729 25.912456,1.0841802E-08 30.948448,0z" Stretch="Uniform" Fill="#FF666666" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
      <Path.RenderTransform>
        <TransformGroup>
          <TransformGroup.Children>
            <RotateTransform Angle="0" />
            <ScaleTransform ScaleX="1" ScaleY="1" />
          </TransformGroup.Children>
        </TransformGroup>
      </Path.RenderTransform>
    </Path>
  </Grid>
</Viewbox>

这里的问题是我正在创建带有图像的按钮,如下所示:

<corecontrols:IdyllicButton Name="btnClose" Height="30">
    <StackPanel Orientation="Horizontal">
        <Image Source="/MHA.Modules.PolicyAdmin;component/Images/Cancel.png"/>
        <TextBlock VerticalAlignment="Center" Margin="5,0" Text="Close"/>
    </StackPanel>
</corecontrols:IdyllicButton>

但我无法将我的Image.ImageSource 设置为我在ResourceDictionary 中创建的StaticResource,因为我收到以下错误:

“System.Windows.Controls.Viewbox”类型的对象不能 应用于期望类型的属性 “System.Windows.Media.ImageSource”。

我也尝试过使用画布。

任何想法如何做到这一点?

【问题讨论】:

  • 我觉得不需要可以直接在StackPanel里面添加Viewbox
  • 如果要将其用作图像源,请单击“导出”按钮(Metro studio),它将询问格式和文件名,将其保存为png。

标签: wpf image xaml button canvas


【解决方案1】:

希望这对某人有所帮助:

我找到了解决方案,而不是使用Image,而是使用Rectangle 控件并将OpacityMask 设置为Viewbox 资源。

<Rectangle Width="15" VerticalAlignment="Center"
        Height="15"
        Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
    <Rectangle.OpacityMask>
        <VisualBrush Stretch="Uniform" Visual="{StaticResource Close}"/>
    </Rectangle.OpacityMask>
</Rectangle>

我找到了WindowCommand 按钮的滚动MahApps.Metro 源代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2013-02-16
    • 2021-06-20
    • 2013-08-05
    • 2019-08-09
    相关资源
    最近更新 更多