最近在完成一個項目,說白了也就是一個在win7上運行的沙盒程序。於是採用WPF + SurfaceSDK實現相應的功能。使用了WPF原生的Button等控件,可是到了客戶機上運行時候控件的樣式總是不是非常理想。後來發現是開發環境和運行環境的Windows主題不同造成的問題,所以準備自己寫一些控件,這個時候就出現了這個奇怪的問題,在此記錄,如有能夠**“正確解決”**的朋友,歡迎留言討論。
錯誤描述: “{DependencyProperty.UnsetValue}”不是 Setter 上“System.Windows.Controls.Border.BorderBrush”屬性的有效值
詳細圖片如下:
項目目錄如下:
#####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" Style="{StaticResource ResourceKey=MyWpfButton}" 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內部註釋掉的代碼,使用靜態資源會報錯。即不能使用靜態資源去實現,只能使用固定的值。