wpf mvvm 数据绑定数据(按钮文字表头都可以),根据长度进行换行,并把换行的文字居中

今天遇到了一个问题,就是数据表头按钮的文字换行后不能居中,如何查找资料后,也是挺简单的,就是绑定控件的文字,进行进行操作,下来我们以按钮为例。

在WPF中使用MVVM模式时,可以通过绑定按钮的文字,并根据文字长度进行换行,同时确保文字居中显示。这通常涉及到自定义控件模板和样式,以及使用数据绑定来动态设置按钮的内容。

以下是一个示例,展示了如何实现这一功能:

定义ViewModel:

首先,定义一个ViewModel,它包含按钮的文本属性。

clike 复制代码
using System.ComponentModel;
using System.Runtime.CompilerServices;
 
public class MainViewModel : INotifyPropertyChanged
{
    private string _buttonText;
 
    public string ButtonText
    {
        get { return _buttonText; }
        set
        {
            _buttonText = value;
            OnPropertyChanged();
        }
    }
 
    public MainViewModel()
    {
        ButtonText = "这是一个非常长的按钮文字,需要进行换行处理,并居中显示。";
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

定义View:

在XAML中定义按钮,并使用数据绑定来设置按钮的内容。为了处理换行和居中,你可以使用TextBlock作为按钮的内容,并设置相应的样式。

clike 复制代码
<Window x:Class="WpfApp.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="400">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <Button Width="200" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button.Content>
                <TextBlock TextWrapping="Wrap"
                           TextAlignment="Center"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Text="{Binding ButtonText}"
                           Margin="10"/>
            </Button.Content>
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Content="{TemplateBinding Content}"
                                          RecognizesAccessKey="True"/>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</Window>

在这个示例中:

  • TextBlock 被用作按钮的内容,并设置了 TextWrapping="Wrap" 和 TextAlignment="Center" 来实现换行和居中。
  • Margin 属性被添加到 TextBlock 上,以确保文本不会紧贴按钮边框。
  • 自定义了按钮的 ControlTemplate,以确保内容(即 TextBlock)在按钮内部居中显示。

运行应用:

运行你的WPF应用,你应该会看到一个按钮,其文字根据长度自动换行,并且文字在按钮内部居中显示。

这个示例展示了如何在MVVM模式下,通过数据绑定和自定义控件模板来实现按钮文字的换行和居中显示。你可以根据需要进一步调整样式和布局。

是不是很简单,我们只需要根据自己的需求进行修好

相关推荐
mudtools10 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫14 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
玉面小君2 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:Trigger、MultiTrigger、DataTrigger 的迁移
wpf·avalonia