GG.*_*GG. 35 wpf formatting xaml textblock
我想在所有文本TextBlock,Label,MenuItem.Header以大写形式显示.字符串取自ResourceDictionary例如:
<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>
Run Code Online (Sandbox Code Playgroud)
等(也用于Label和其他控制)
我不能使用值转换器,因为没有绑定.我不想在字典本身中使字符串大写.
Pet*_*ter 35
您仍然可以使用转换器,只需在绑定源中设置textvalue:
<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
scr*_*789 30
您可以在TextBox中使用标记CharacterCasing,而不是使用转换器,但在您的情况下,它不适用于TextBlock.
<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />
Run Code Online (Sandbox Code Playgroud)
Ali*_*ese 25
我认为这对你有用
<TextBlock Text='{StaticResource String1}' Typography.Capitals="AllSmallCaps"/>
Run Code Online (Sandbox Code Playgroud)
对于字体大写枚举https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx
Max*_*Max 12
要完成Peter的回答(我的编辑已被拒绝),您可以使用这样的转换器:
C#:
public class CaseConverter : IValueConverter
{
public CharacterCasing Case { get; set; }
public CaseConverter()
{
Case = CharacterCasing.Upper;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var str = value as string;
if (str != null)
{
switch (Case)
{
case CharacterCasing.Lower:
return str.ToLower();
case CharacterCasing.Normal:
return str;
case CharacterCasing.Upper:
return str.ToUpper();
default:
return str;
}
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myCaseConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
小智 7
我为此创建了附加属性和转换器.您可能已经拥有转换器,因此将我对CaseConverter的引用替换为您拥有的任何实现.
如果你希望它是大写的,那么附加属性只是你设置的一个布尔值(显然你可以将它扩展为可选择的样式的枚举).当属性更改时,它会根据需要重新绑定TextBlock的Text属性,并添加转换器.
当属性已经绑定时,可能需要做更多的工作 - 我的解决方案假设它是一个简单的Path绑定.但它可能还需要复制源等.但我觉得这个例子足以说明我的观点.
这是附属物:
public static bool GetUppercase(DependencyObject obj)
{
return (bool)obj.GetValue(UppercaseProperty);
}
public static void SetUppercase(DependencyObject obj, bool value)
{
obj.SetValue(UppercaseProperty, value);
}
// Using a DependencyProperty as the backing store for Uppercase. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UppercaseProperty =
DependencyProperty.RegisterAttached("Uppercase", typeof(bool), typeof(TextHelper), new PropertyMetadata(false, OnUppercaseChanged));
private static void OnUppercaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBlock txt = d as TextBlock;
if (txt == null) return;
var val = (bool)e.NewValue;
if (val)
{
// rebind the text using converter
// if already bound, use it as source
var original = txt.GetBindingExpression(TextBlock.TextProperty);
var b = new Binding();
if (original != null)
{
b.Path = original.ParentBinding.Path;
}
else
{
b.Source = txt.Text;
}
b.Converter = new CaseConverter() { Case = CharacterCasing.Upper };
txt.SetBinding(TextBlock.TextProperty, b);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72594 次 |
| 最近记录: |