c# - WPF 按钮模板中的矢量图像并不总是显示

标签 c# wpf xaml dependency-properties controltemplate

我有一个带有几个按钮的 WPF 应用程序,这些按钮上没有文本,只有基于矢量的图像(使用 Path 对象),ControlTemplate 如下所示:

<ControlTemplate x:Key="IconButtonContentTemplate" TargetType="{x:Type ButtonBase}">
    <Grid Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, Mode=FindAncestor}}">
        <Path HorizontalAlignment="Center" VerticalAlignment="Center" 
            Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, Mode=FindAncestor}}" 
            Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, Mode=FindAncestor}}"   
            Data="{Binding (components:ImageButtonAttachedProperties.ImagePathData), RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, Mode=FindAncestor}}"
            Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, Mode=FindAncestor}}" />
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="LightGray" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Foreground" Value="LightGray" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Gray" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate> 


<Style TargetType="{x:Type Button}" x:Key="ClockButtonStyle" BasedOn="{StaticResource IconButtonStyle}">
    <Setter Property="Template" Value="{StaticResource IconButtonContentTemplate}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="components:ImageButtonAttachedProperties.ImagePathData" Value="M69.349,65.092C68.714,65.092,68.072,64.925,67.487,64.577L46.294,51.946 46.294,21.239C46.294,19.227 47.925,17.595 49.938,17.595 51.949,17.595 53.581,19.227 53.581,21.239L53.581,47.807 71.217,58.317C72.945,59.348 73.512,61.585 72.483,63.312 71.799,64.457 70.589,65.092 69.349,65.092z M49.938,3.877C24.573,3.877 3.938,24.513 3.938,49.877 3.938,75.241 24.573,95.877 49.938,95.877 75.302,95.877 95.938,75.241 95.938,49.877 95.938,24.513 75.302,3.877 49.938,3.877z M52.876,88.467C52.882,88.395 52.897,88.324 52.897,88.25 52.897,86.615 51.572,85.29 49.937,85.29 48.302,85.29 46.977,86.615 46.977,88.25 46.977,88.324 46.994,88.395 46.999,88.467 27.994,87.032 12.783,71.822 11.349,52.817 11.423,52.822 11.492,52.838 11.567,52.838 13.202,52.838 14.527,51.513 14.527,49.878 14.527,48.243 13.201,46.918 11.567,46.918 11.492,46.918 11.422,46.935 11.349,46.94 12.783,27.933 27.994,12.722 47,11.287 46.995,11.36 46.978,11.43 46.978,11.504 46.978,13.139 48.304,14.464 49.938,14.464 51.572,14.464 52.897,13.138 52.897,11.504 52.897,11.429 52.881,11.36 52.876,11.287 71.882,12.722 87.093,27.932 88.528,46.938 88.455,46.933 88.385,46.916 88.311,46.916 86.676,46.916 85.35,48.242 85.35,49.876 85.35,51.51 86.676,52.836 88.311,52.836 88.385,52.836 88.455,52.82 88.528,52.815 87.094,71.822 71.883,87.032 52.876,88.467z" />
</Style>

我的问题是,在极少数情况下,按钮图像不会显示(99% 的情况下都会显示)。仍然可以单击该按钮,但不显示其上的图像。 我不确定是什么原因造成的。矢量图像上的数据绑定(bind)?或者填充颜色上的数据绑定(bind)?

ImageButtonAttachedProperties.ImagePathData 是 System.Windows.Media.Geometry 对象,它描述矢量图像。

public static class ImageButtonAttachedProperties
{
    public static readonly DependencyProperty ImagePathDataProperty =
        DependencyProperty.RegisterAttached("ImagePathData", typeof(Geometry), typeof(ImageButtonAttachedProperties), new UIPropertyMetadata(null));

    public static Geometry GetImagePathData(DependencyObject obj)
    {
        return (Geometry)obj.GetValue(ImagePathDataProperty);
    }

    public static void SetImagePathData(DependencyObject obj, Geometry value)
    {
        obj.SetValue(ImagePathDataProperty, value);
    }
}

知道我在这里做错了什么吗?

最佳答案

您将模板元素的所需尺寸绑定(bind)到模板化父元素的最终尺寸;这会产生循环依赖。不要这样做。

模板化控件(例如,您的 Button)的大小取决于其模板内容(例如,您的 Path)所需的大小。如果您使内容的大小取决于模板化父级的大小,那么您只是自找麻烦。

如果我不得不猜测,您可能会陷入布局循环,其中一个布局更新会在下一个更新时触发另一个布局更新。由于布局更新的安排方式,这些并不总是显而易见的(也许除了查看 CPU 使用率)。我以前见过这种情况发生过,例如,当“排列”过程使“测量”过程的结果无效时。可见的效果可能相当不可预测:有时事情看起来工作正常,直到它们不起作用。

删除 Path 上的 WidthHeight 绑定(bind),并将水平和垂直对齐设置为 Stretch.

关于c# - WPF 按钮模板中的矢量图像并不总是显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53033980/

相关文章:

c# - 如何优化此 Linq 查询以查找过去 24 小时内浏览次数最多的博客文章

c# - 正则表达式可以从字符串中提取多个数字吗?

c# - 从 WPF System.Windows.Shapes.Path 创建图像

c# - 是否可以在装饰层之上绘制 UI 元素?

C# Entity Framework 未初始化数据库

wpf - 使用 XAML 矢量图像作为窗口图标

c# - 使用 WPF 和 XAML 创建图像的滚动网格

c# - 使用 MVC3 和 EF4 CTP5w 进行 JSON 序列化的循环引用异常

c# - 如何在 ASP.net Core 2 中创建动态 API

wpf - xaml 中数据模板的设计时数据