我创建了一个按钮,它应该支持自动换行。我的按钮 XAML 代码如下所示:
<Button x:Class="POS.TouchScreen.UI.Elements.TouchButtonWPF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="23" HorizontalAlignment="Left" Name="buttonGrid" VerticalAlignment="Top" Width="75" BorderBrush="#FF8A97A9" Margin="4"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBlock Name="ButtonTextBlock"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Text="{Binding ButtonText, Mode=TwoWay}"
TextWrapping="Wrap">
</TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)
我已经实现了该属性,如下所示:
public static readonly DependencyProperty ButtonTextProperty =
DependencyProperty.Register("ButtonText", typeof(string), typeof(TouchButtonWPF), new UIPropertyMetadata("Button",new PropertyChangedCallback(OnButtonTextChanged), new CoerceValueCallback(OnCoerceButtonText)));
private static object OnCoerceButtonText(DependencyObject o, object value)
{
TouchButtonWPF button = o as TouchButtonWPF;
if (button != null)
return button.OnCoerceButtonText((string)value);
else
return value;
}
protected virtual string OnCoerceButtonText(string value)
{
return value;
}
private static …Run Code Online (Sandbox Code Playgroud)