C# WPF自定义文本块布局
我是WPF新手,我想知道是否有可能创建一个自定义文本块,以便以某种方式呈现字符串 因此,对于任何给定字符串: 以最多32个字符或任意长度包装字符串 在第四个和第五个字符之间插入空格 在第六个和第七个字符之间插入制表符 在换行之前,在线的末端添加垂直管道。 这个列表还在继续,本质上我想知道如果我创建一个从TextBlock继承的自定义TextBlock,我是否可以以某种方式覆盖默认行为?您可以编写一个转换TextBlock内容的脚本。文本: 然后在窗口或用户控件中使用转换器: MyNamespace.MyWindowClass.xaml.cs MyNamespace.MyWindowClass.xaml 您可以编写一个转换TextBlock内容的 然后在窗口或用户控件中使用转换器: MyNamespace.MyWindowClass.xaml.cs MyNamespace.MyWindowClass.xamlC# WPF自定义文本块布局,c#,wpf,C#,Wpf,我是WPF新手,我想知道是否有可能创建一个自定义文本块,以便以某种方式呈现字符串 因此,对于任何给定字符串: 以最多32个字符或任意长度包装字符串 在第四个和第五个字符之间插入空格 在第六个和第七个字符之间插入制表符 在换行之前,在线的末端添加垂直管道。 这个列表还在继续,本质上我想知道如果我创建一个从TextBlock继承的自定义TextBlock,我是否可以以某种方式覆盖默认行为?您可以编写一个转换TextBlock内容的脚本。文本: 然后在窗口或用户控件中使用转换器: MyNamespac
非常感谢,看起来是我需要的。非常感谢,看起来是我需要的。
public class MyTextConverter : IValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (!string.IsNullOrEmpty(values?[0]))
{
var manipulatedString = values[0] as string;
// Do something with the string...
return manipulatedString;
}
return string.Empty;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class MyWindowClass : Window
{
public MyWindowClass() {}
public MyTextProperty { get; set; }
}
<Window
x:Class="MyNamespace.MyWindowClass"
xmlns:local="clr-namespace:MyNamespace"
DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
<Window.Resources>
<local:MyTextConverter x:Key="myTextConverterInstance"/>
</Window.Resources>
<!-- ... -->
<TextBlock Text="{Binding MyTextProperty,
Converter={StaticResource myTextConverterInstance}}"/>
<!-- ... -->
</Window>