简体   繁体   English

只有一个 TextBlock 的用户控件。 如何向消费者公开 TextWrapping 属性?

[英]A Usercontrol with just a TextBlock. How do I expose the TextWrapping property to the consumer?

I have a minimalist UserControl containing a single TextBlock which I prototype as follows:我有一个简约的UserControl ,其中包含一个TextBlock ,我的原型如下:

<Grid>
    <TextBlock x:Name="textBlockExt" x:FieldModifier="public"/>
</Grid>
public partial class TextBlockExt : UserControl
{
    public TextBlockExt()
    {
        InitializeComponent();

        DataContext = this;
    }

    public string Text
    {
        get => textBlockExt.Text;
        set { textBlockExt.Text = value.ToUpper(); } // to be expanded later
    }
}

I then consume the control by然后我通过

<Grid>
    <StackPanel>
        <local:TextBlockExt FontSize="30" x:Name="txb" Text="Hello World" TextBlock.TextAlignment="Center"/>
        <Button Content="To Upper" Width="100" Click="Button_Click"/>
    </StackPanel>
</Grid>

And it's code behind is它背后的代码是

public MainWindow()
{
    InitializeComponent();

    txb.textBlockExt.TextWrapping = TextWrapping.WrapWithOverflow;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    txb.Text = "one two three four five six seven eight";
}

I don't like the code in the MainWindow constructor above and need to know how to set the T extWrapping in the XAML.我不喜欢上面MainWindow构造函数中的代码,需要知道如何在 XAML 中设置 T extWrapping

Add a property TextWrapping to your TextBlockExt class.将属性 TextWrapping 添加到您的 TextBlockExt class。 Use the same way you did for the Text property:使用与 Text 属性相同的方式:

public TextWrapping TextWrapping {
    get => txtBlockExt.TextWrapping;
    set { txtBlockExt.TextWrapping = value: }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取带有文本包装的文本块以填充视图框 - How do I get a textblock with textwrapping to fill a viewbox 如何在TextBlock中设置文本。 - How to set a text in TextBlock. 根据TextWrapping属性获取TextBlock的行? - Get the lines of the TextBlock according to the TextWrapping property? 如何将 UserControl 中的嵌套控件完全暴露给外部? - How do I fully expose a nested control in a UserControl to the outside? 如何更改 WPF 中 Textblock 的文本属性? - How do I change the text property of a Textblock in WPF? 应用 TextWrapping 后如何获得具有内联元素支持的 TextBlock 行? - How to get TextBlock lines with Inline element support after applying TextWrapping? WPF C# 文本块。 如何以编程方式格式化文本块文本以格式化某些颜色名称的单词的前景色 - WPF C# TextBlock. How to format textblock text programatically to format foreground color of certain words that are color names 如何正确公开自定义控件上的属性? - How do I correctly Expose a property on a custom control? 如何通过字符串正确访问UserControl公共属性 - How Do I Properly Access a UserControl Public Property by String 如何为ASPX UserControl属性设置默认值? - How do I set a default value for a ASPX UserControl property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM