https://www.cnblogs.com/xpvincent/p/4475679.html
xaml
<Window x:Class="WpfButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfButton"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Width="200" Height="100" x:Name="test" Style="{StaticResource button}"/>
</Grid>
</Window>
style
在ControlTemplate 使用TemplateBinding 获取button的content
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfButton">
<local:ContentToColor x:Key="KContentToColor" />
<local:ContentToString x:Key="KContentToString" />
<local:ContentToHMString x:Key="KContentToHMString" />
<Style x:Key="button" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="MainBorder" CornerRadius="3" MinWidth="100"
MinHeight="24" BorderBrush="#d7e1ec" BorderThickness="0" Background="Transparent">
<Border.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="Black" Text="通信延迟:"/>
<TextBlock Foreground="Black" Text="{TemplateBinding Content,Converter={StaticResource KContentToHMString}}"/>
</StackPanel>
</Border.ToolTip>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Image Height="28" Margin="0,0,5,0" Source="{TemplateBinding Content,Converter={StaticResource KContentToColor}}"/>
<ContentPresenter Content="{TemplateBinding Content,Converter={StaticResource KContentToString}}" />
</StackPanel>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="{StaticResource MenuBtnMouseMove}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
converter
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace WpfButton
{
public class ContentToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object color = Binding.DoNothing;
if (value == null) return color;
string onLine = value.ToString();
long lDelay = 0;
//if (onLine.Equals("通信正常"))
//{
// color = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#37c200"));
// //color = new SolidColorBrush(Colors.Red);
//}
//else
//{
// // color = new SolidColorBrush(Colors.Orange);
// color = new SolidColorBrush(Colors.Red);
//}
if (onLine.Contains("通信异常"))
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Communication/通信异常.png"));
}
else
{
if (long.TryParse(onLine, out lDelay))
{
/*
通信正常 0<=x<=50 绿
通信一般 50<x<=100 蓝
通信较差 100<x<=300 黄
通信很差 X>300 橙
通信异常 红
*/
if (lDelay <= 50)
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/通信正常.png"));
}
else if (lDelay > 50 && lDelay <= 100)
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/通信一般.png"));
}
else if (lDelay > 100 && lDelay <= 300)
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/通信较差.png"));
}
else
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/通信很差.png"));
}
}
else
{
color = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/通信异常.png"));
}
}
return color;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class ContentToString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sContent = "";
if (value == null) return "";
string onLine = value.ToString();
long lDelay = 0;
if (onLine.Contains("通信异常"))
{
sContent = value.ToString();
}
else
{
if (long.TryParse(onLine, out lDelay))
{
/*
通信正常 0<=x<=50 绿
通信一般 50<x<=100 蓝
通信较差 100<x<=300 黄
通信很差 X>300 橙
通信异常 红
*/
if (lDelay <= 50)
{
sContent = "通信正常";
}
else if (lDelay > 50 && lDelay <= 100)
{
sContent = "通信一般";
}
else if (lDelay > 100 && lDelay <= 300)
{
sContent = "通信较差";
}
else
{
sContent = "通信很差";
}
}
else
{
sContent = value.ToString();
}
}
return sContent;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class ContentToHMString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sContent = "";
if (value == null) return "";
string onLine = value.ToString();
long lDelay = 0;
if (onLine.Contains("通信异常"))
{
sContent = "------ ms";
}
else
{
if (long.TryParse(onLine, out lDelay))
{
/*
通信正常 0<=x<=50 绿
通信一般 50<x<=100 蓝
通信较差 100<x<=300 黄
通信很差 X>300 橙
通信异常 红
*/
//if (lDelay <= 50)
//{
// sContent = "通信正常";
//}
//else if (lDelay > 50 && lDelay <= 100)
//{
// sContent = "通信一般";
//}
//else if (lDelay > 100 && lDelay <= 300)
//{
// sContent = "通信较差";
//}
//else
//{
// sContent = "通信很差";
//}
sContent = lDelay.ToString() + " ms";
}
else
{
sContent = "------ ms";
}
}
return sContent;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
**new BitmapImage(new Uri(“pack://application:,/Resources/Images/通信很差.png”));**使用这种方式获取图片