0

我试图ActualWidthTextBox使用. 这是:ConverterTemplateBindingXAML

<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();
    }
}

每次我运行这个应用程序valueFullWidthToContentWidthConverter.Convert都是 equal 0,这是不正确的。我也尝试ActualWidthWidththen value 替换为等于NaN,但事实并非如此。我怎样才能通过真实的ActualWidth

4

0 回答 0