I tried to convert upper case to lower case by XAML in WPF like below:
<TextBox Height="86" CharacterCasing="Upper"/>
I want to achieve the same scenario with TextBlock
, Label
and Button
.
How can I do it?
I tried to convert upper case to lower case by XAML in WPF like below:
<TextBox Height="86" CharacterCasing="Upper"/>
I want to achieve the same scenario with TextBlock
, Label
and Button
.
How can I do it?
You should use a value converter:
public class ToUpperValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var str = value as string;
return string.IsNullOrEmpty(str) ? string.Empty : str.ToUpper();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
One way is to do this is to use NotifyOnTargetUpdated
and handle TextChanged
event.
XAML
<TextBlock Name="TB" Text="{Binding Path=YourProperty, NotifyOnTargetUpdated=True}"
TargetUpdated="TB_TargetUpdated" />
Code behind
private void TB_TargetUpdated(object sender, DataTransferEventArgs e)
{
TB.Text = TB.Text.ToUpper();
}
Just take a look at that: How to make all text upper case / capital?.
More generally, each time you want to transform a value to go into a control, think of a converter and write it yourself (or use it if it already exists).
You can find additional documentation on converters here: http://wpftutorial.net/ValueConverters.html.