首页 > 其他分享 >WPF Customize Button ControlTemplate TextBlock

WPF Customize Button ControlTemplate TextBlock

时间:2024-09-03 17:25:40浏览次数:12  
标签:DependencyProperty Customize System BtnTbk TextBlock Windows using WPF public

//xaml
<UserControl x:Class="WpfApp332.BtnTbk"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp332"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Width="200" Height="50" Background="{Binding BgBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <Button.Template>
                <ControlTemplate>
                    <TextBlock Text="{Binding TbkStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Left"/>
                </ControlTemplate>
            </Button.Template>
        </Button>        
    </Grid>
</UserControl>


//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp332
{
    /// <summary>
    /// Interaction logic for BtnTbk.xaml
    /// </summary>
    public partial class BtnTbk : UserControl
    {
        public BtnTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public SolidColorBrush BgBrush
        {
            get { return (SolidColorBrush)GetValue(BgBrushProperty); }
            set { SetValue(BgBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BgBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BgBrushProperty =
            DependencyProperty.Register("BgBrush", typeof(SolidColorBrush), 
                typeof(BtnTbk), new PropertyMetadata(new SolidColorBrush(Colors.Red)));



        public string TbkStr
        {
            get { return (string)GetValue(TbkStrProperty); }
            set { SetValue(TbkStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TbkStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TbkStrProperty =
            DependencyProperty.Register("TbkStr", typeof(string), 
                typeof(BtnTbk), new PropertyMetadata(""));




    }
}

 

 

 

 

//xaml
<UserControl x:Class="WpfApp332.BtnTbk"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp332"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Width="200" Height="50" Background="{Binding BgBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <Button.Template>
                <ControlTemplate>
                    <TextBlock Text="{Binding TbkStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Left"/>
                </ControlTemplate>
            </Button.Template>
        </Button>        
    </Grid>
</UserControl>



//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp332
{
    /// <summary>
    /// Interaction logic for BtnTbk.xaml
    /// </summary>
    public partial class BtnTbk : UserControl
    {
        public BtnTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public SolidColorBrush BgBrush
        {
            get { return (SolidColorBrush)GetValue(BgBrushProperty); }
            set { SetValue(BgBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BgBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BgBrushProperty =
            DependencyProperty.Register("BgBrush", typeof(SolidColorBrush), 
                typeof(BtnTbk), new PropertyMetadata(new SolidColorBrush(Colors.Red)));



        public string TbkStr
        {
            get { return (string)GetValue(TbkStrProperty); }
            set { SetValue(TbkStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TbkStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TbkStrProperty =
            DependencyProperty.Register("TbkStr", typeof(string), 
                typeof(BtnTbk), new PropertyMetadata(""));




    }
}







<Window x:Class="WpfApp332.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:WpfApp332"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <UniformGrid x:Name="uniformGrid">
         
    </UniformGrid>
</Window>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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 WpfApp332
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random rnd { get; set; }
        public MainWindow()
        {
            rnd = new Random();
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            for(int i=0;i<200;i++)
            {
                BtnTbk btn = new BtnTbk();
                btn.Background = new SolidColorBrush(Color.FromRgb((byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255)));
                btn.TbkStr=(i+1).ToString();
                if(!uniformGrid.Children.Contains(btn))
                {
                    uniformGrid.Children.Add(btn);
                }
            }
        } 
    }
}

 

 

标签:DependencyProperty,Customize,System,BtnTbk,TextBlock,Windows,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18395031

相关文章

  • WPF UniformGrid contain children auto resize
    //xaml<Windowx:Class="WpfApp332.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF性能优化之UI虚拟化
    @目录前言一、VirtualizingStackPanel1.1虚拟化功能介绍1、在Window中添加一个ListBox控件。2、在设计视图中用鼠标选中ListBox控件并右健依次单击“编辑其他模板”-“编辑项的布局模板”-“编辑副本”。3、查看生成的模板代码。1.2虚拟化参数介绍二、CustomVirtualizingPanel2.1......
  • WPF性能优化之UI虚拟化
    前言相信很多WPF开发者都碰到过这种情况,当在一个ItemsControl(或继承自ItemsControl)控件中绑定一个集合的时候,如果集合中的条目过多,那么界面就会变得卡顿甚至停止响应,特别是在容器或窗口大小发生改变时,界面的渲染就会给人一种慢半拍的感觉,体验感非常差,这时我们就可以用虚拟化技术......
  • wpf 外部样式实现方式
    最近开发一个产品,打算把每个功能模块单独写一个DLL,来实现复用。那么问题来了,每个DLL样式都是类似的,每个DLL里面都搞样式,不利于后期的调整。所以呢把样式单独的放到一个DLL中。实现大致如下:1、新建自定义控件库 StyleLibrary放样式Styles.xaml<ResourceDictionaryxmlns="ht......
  • WPF communicate across different modules via event
    //Runtimeproject,cclasslibraryusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Input;namespaceRuntime{publicclassDelCmd:......
  • WPF access mysql and pass data from datagrid to mysql
    //sqldropdatabaseifexistsmydb;createdatabasemydb;usemydb;droptableifexistsmt;createtablemt(idintauto_incrementprimarykey,namevarchar(50)notnulldefault'',isbnvarchar(50)notnulldefault'',authorvarchar......
  • WPF—LiveCharts图表
    LiveCharts图表LiveCharts是一个简单灵活、交互式以及功能强大的跨平台图表库,支持wpf、winform...应用程序。快速入门安装在应用程序中右键`引用`​,点击`管理NuGet程序包`​,选择`浏览`​,搜索`LiveChartsCore.SkiaSharpView.WPF`​,注意必须勾选`包括预发行版`​选项,否则搜索......
  • WPF Material Design中资源的查找和使用
    MaterialDesign中,一共分为两大块。一个是颜色资源,一个是控件资源。下面来说下,如何使用控件资源:在VS中,通过Nuget添加完MaterialDesign后,还需要在App.xaml中引用这些资源,引用的方法如下图所示:在1处,引入materialdesign的引用。在2处,可以修改项目的主题色,这个比较重要。以前......
  • WPF中如何根据数据类型使用不同的数据模板
    我们在将一个数据集合绑定到列表控件时,有时候想根据不同的数据类型,显示为不同的效果。例如将一个文件夹集合绑定到ListBox时,系统文件夹和普通文件夹分别显示为不同的效果,就可以使用模板选择器功能。WPF提供了一个模板选择器类型DataTemplateSelector,它可以根据数据对象和数据......
  • .NET|--WPF|--笔记合集|--依赖项属性|--2.注册依赖项属性
    前言使用一个实例,其实分2步骤定义,实例化.但是依赖项属性为不能直接实例化,因为DependencyProperty类没有公开的构造函数,只能使用静态的DependencyProperty.Register方法创建DependencyProperty实例.DependencyProperty.Register源码//System.Windows.DependencyP......