[英]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.