我试图ActualWidth
在TextBox
使用. 这是:Converter
TemplateBinding
XAML
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<TextBlock Text="{TemplateBinding Text}" Width="{TemplateBinding ActualWidth , Converter={StaticResource FullWidthToContentWidthConverter}, ConverterParameter='10'}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
这是转换器本身:
public class FullWidthToContentWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fullWidth = double.Parse(value.ToString());
var widthToSubtract = double.Parse(parameter.ToString());
return fullWidth - widthToSubtract;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
每次我运行这个应用程序value
时FullWidthToContentWidthConverter.Convert
都是 equal 0
,这是不正确的。我也尝试ActualWidth
用Width
then value 替换为等于NaN
,但事实并非如此。我怎样才能通过真实的ActualWidth
?