0

I'm trying to pass ActualWidth of TextBox to Converter when binding using TemplateBinding. This is the 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>

And this is the converter itself:

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

Every time I run this app value that comes to FullWidthToContentWidthConverter.Convert is equal 0, which is not true. I also tried to replace ActualWidth with Width then value became equal to NaN, which is also not the case. How can I pass real ActualWidth?

Mykhailo Seniutovych
  • 3,527
  • 4
  • 28
  • 50
  • Try with normal binding + templated parent. Template binding is optimized, so maybe this fails in your case. – sTrenat Dec 22 '17 at 14:40
  • @sTrenat I tried, no success – Mykhailo Seniutovych Dec 22 '17 at 15:03
  • @sTrenat could you elaborate please, and point out the right way? I'm trying to get the actual width of `TextBox` which this `TextBlock` is being template for. – Mykhailo Seniutovych Dec 22 '17 at 16:03
  • 1
    Ok, i just realized, you'r doing this in trigger. I tried your code and it works for me. Obviously, on init it's 0, but after init, it's fire 2nd time and then it's size is ok. Are you sure this don't work for you? Can you give us more code? – sTrenat Dec 22 '17 at 16:15
  • @sTrenat Why is it 0 on init and is there a way to pass an actual width to converter when the form gets initialized? – Mykhailo Seniutovych Dec 22 '17 at 16:18
  • Do you realy need width when ekement is not even loaded? can you see any weird behaviour on UI, or just 0 when in breakpoint? For me, you'r sample code work as expected. – sTrenat Dec 22 '17 at 16:37
  • @sTrenat This is just a sample code I used to demonstrate the issue I have. In reality in this line `` I use my custom control that inherits from `TextBlock` and I need to pass `TemplateBinding` width to this element to do some operations inside my control later on. – Mykhailo Seniutovych Dec 22 '17 at 16:47
  • Can you provide code, that will allow to reconstruct issue? It's realy hard to guess what's wrong when I can't see it. – sTrenat Dec 22 '17 at 16:52
  • It is 0 since the `TextBox` control is not rendered yet. If you pass a "default value" during the control initialization, it will be overwritten by the real `ActualWidth` value immediately after. So why do you need it? – Il Vic Dec 22 '17 at 17:03

0 Answers0