C# 按钮首字母的样式格式

C# 按钮首字母的样式格式,c#,xaml,format,C#,Xaml,Format,希望将按钮的前3个字母设置为粗体和下划线 <Button Content="{Binding Path=ButtonText}"/> 你知道怎么做吗?显然不是用字符串格式…嗨,我认为完全用XAML是不可能做到这一点的,但是在C#的帮助下,这是可能的。我会这样做: 首先,我创建了一个名为DummyData的类,其中包含一个属性文本,然后我将该属性划分为两个单独的属性。第一个仅包含我文本的前3个字母,另一个包含其余字母: public DummyData TestData { ge

希望将按钮的前3个字母设置为粗体和下划线

<Button Content="{Binding Path=ButtonText}"/>


你知道怎么做吗?显然不是用字符串格式…

嗨,我认为完全用XAML是不可能做到这一点的,但是在C#的帮助下,这是可能的。我会这样做:

首先,我创建了一个名为DummyData的类,其中包含一个属性文本,然后我将该属性划分为两个单独的属性。第一个仅包含我文本的前3个字母,另一个包含其余字母:

public DummyData TestData { get; set; }

public MainWindow()
{

    TestData = new DummyData() { Text = "This is a test data." };

    InitializeComponent();
}

public class DummyData
{
    public string Text { get; set; }

    public string FirstThreeLetters
    {
        get
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(Text))
            {
                result = Text.Substring(0, 3);
            }

            return result;
        }
    }

    public string RestOfTheText
    {
        get
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(Text))
            {
                result = Text.Substring(3);
            }

            return result;
        }
    }
}
最后,我们将在XAML中统一这些属性,如下所示:

<Window
    x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    mc:Ignorable="d">
    <Grid>
        <Button Width="200" Height="100">
            <TextBlock>
                <Run FontWeight="Bold" Text="{Binding TestData.FirstThreeLetters, Mode=OneWay}" /><Run Text="{Binding TestData.RestOfTheText, Mode=OneWay}" />
            </TextBlock>
        </Button>
    </Grid>
</Window>


请注意DataContext,因为这就是我从代码背后绑定数据的方式。希望这能有所帮助。

嗨,我认为在XAML中完全实现这一点是不可能的,但是在C#的帮助下,这是可能的。我会这样做:

首先,我创建了一个名为DummyData的类,其中包含一个属性文本,然后我将该属性划分为两个单独的属性。第一个仅包含我文本的前3个字母,另一个包含其余字母:

public DummyData TestData { get; set; }

public MainWindow()
{

    TestData = new DummyData() { Text = "This is a test data." };

    InitializeComponent();
}

public class DummyData
{
    public string Text { get; set; }

    public string FirstThreeLetters
    {
        get
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(Text))
            {
                result = Text.Substring(0, 3);
            }

            return result;
        }
    }

    public string RestOfTheText
    {
        get
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(Text))
            {
                result = Text.Substring(3);
            }

            return result;
        }
    }
}
最后,我们将在XAML中统一这些属性,如下所示:

<Window
    x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    mc:Ignorable="d">
    <Grid>
        <Button Width="200" Height="100">
            <TextBlock>
                <Run FontWeight="Bold" Text="{Binding TestData.FirstThreeLetters, Mode=OneWay}" /><Run Text="{Binding TestData.RestOfTheText, Mode=OneWay}" />
            </TextBlock>
        </Button>
    </Grid>
</Window>


请注意DataContext,因为这就是我从代码背后绑定数据的方式。希望这有帮助。

你好,哈桑。非常感谢,这是一个很好的解决方案。我已经实现了它,虽然屏幕上有一个问题,但是单词的前半部分和后半部分之间有一些空格。我不明白为什么(没有找到空白)很高兴你发现它很有用。你能编辑你的答案并分享你的代码吗?这样我就可以看看发生了什么。只要试着在两次运行之间不要留有空格,如果这不起作用,就把它们放在一行中。请共享您的代码:)。这样就解决了问题,谢谢!现在的问题是按钮的基本样式消失了(启用/禁用时的颜色不一样)注意:我可以使用内置TexBlocks的WrapPanel进行同样的操作。在这种情况下,不需要像运行一样将这两个块放在同一条线上。但是没有修复样式。在这种情况下,你必须与样式共享代码才能得到正确的答案。嗨,哈桑。非常感谢,这是一个很好的解决方案。我已经实现了它,虽然屏幕上有一个问题,但是单词的前半部分和后半部分之间有一些空格。我不明白为什么(没有找到空白)很高兴你发现它很有用。你能编辑你的答案并分享你的代码吗?这样我就可以看看发生了什么。只要试着在两次运行之间不要留有空格,如果这不起作用,就把它们放在一行中。请共享您的代码:)。这样就解决了问题,谢谢!现在的问题是按钮的基本样式消失了(启用/禁用时的颜色不一样)注意:我可以使用内置TexBlocks的WrapPanel进行同样的操作。在这种情况下,不需要像运行一样将这两个块放在同一条线上。但是没有修复样式。在这种情况下,为了得到正确的答案,您必须与样式共享代码。