wpf 使用templatebinding,并设置button的背景图片

48 篇文章 2 订阅

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”));**使用这种方式获取图片

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值