首页 » .NET » 一个奇怪的bug

一个奇怪的bug

原文 http://blog.csdn.net/yyhaohaoxuexi/article/details/79228899

2018-02-02 02:00:35阅读(381)

  最近在完成一个项目,说白了也就是一个在win7上运行的沙盒程序。于是采用WPF + SurfaceSDK实现相应的功能。使用了WPF原生的Button等控件,可是到了客户机上运行时候控件的样式总是不是非常理想。后来发现是开发环境和运行环境的Windows主题不同造成的问题,所以准备自己写一些控件,这个时候就出现了这个奇怪的问题,在此记录,如有能够“正确解决”的朋友,欢迎留言讨论。

错误描述: “{DependencyProperty.UnsetValue}”不是 Setter 上“System.Windows.Controls.Border.BorderBrush”属性的有效值
详细图片如下:
一个奇怪的<a href=bug" src="http://img.blog.csdn.net/20180201124857816?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXloYW9oYW94dWV4aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="错误图片" title="">

项目目录如下:
一个奇怪的bug

App.xaml
<Application x:Class="WpfApplication_MyButton.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--自定义颜色-->
    <LinearGradientBrush x:Key="LinearGradientBlueBackground" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF377FED" Offset="0" />
        <GradientStop Color="#FF074CC0" Offset="1" />
    </LinearGradientBrush>
    <!--<Color x:Key="MyBtnHoverBackgroundColor" >#000000</Color>-->
    <!--END-->
    <Style x:Key="MyWpfButton" TargetType="{x:Type Button}" >
        <Setter Property="Background" Value="{StaticResource LinearGradientBlueBackground}"></Setter>
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="BorderBrush" Value="#000000"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                                SnapsToDevicePixels="true" CornerRadius="3,3,3,3">
                        <ContentPresenter x:Name="contentPresenter" 
                                              Focusable="False" 
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              Margin="{TemplateBinding Padding}" 
                                              RecognizesAccessKey="True" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"  />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <!--此处使用静态调用会报错误,必须使用具体的值才行-->
                            <!--<Setter Property="Background" TargetName="border" Value="{StaticResource MyBtnHoverBackgroundColor}"/>-->
                            <Setter Property="Background" TargetName="border" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="WpfApplication_MyButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button1" Height="40" Width="100" Margin="217,40,186,232"></Button>
        <Button Content="Button2" HorizontalAlignment="Left" Height="40" Width="100" Margin="34,39,0,232"></Button>
    </Grid>
</Window>
MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication_MyButton
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

就是Dictionary1.xaml内部注释掉的代码,使用静态资源会报错。即不能使用静态资源去实现,只能使用固定的值。

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹