Materialdesigninxamltoolkit: HintAssist string formatting don't seem to work

Created on 26 Aug 2019  路  3Comments  路  Source: MaterialDesignInXAML/MaterialDesignInXamlToolkit

Specifying a string formatter doesn't seem to work for hint assist.
For instance
materialDesign:HintAssist.Hint="{Binding boundProperty, StringFormat='Default value: {0}'}"
only displays the bound property value not the text 'Default value' before it.

Out of scope question

Most helpful comment

@Awsmolak this is actually by design with WPF.

Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.

The key is that last condition: "if it displays the bound value as a string". It is a bit of poor wording, but it basically means that the StringFormat property only applies if the Binding is being set on a string dependency property.
In this case the Hint property is of type object so the StringFormat property will never work if directly applied. This allows you to specify more than just strings for the hint (for example you could specify a StackPanel that contains a PackItem and TextBlock). @jespersh suggestion above is correct. If you want to use the StringFormat on the Hint property you need to specify the Hint as a TextBlock and bind you string to its Text property.

Something like this:

<TextBox>
    <materialDesign:HintAssist.Hint>
        <TextBlock Text="{Binding MyProperty, StringFormat='Foo: {0}'}" />
    </materialDesign:HintAssist.Hint>
</TextBox>

All 3 comments

Just to verify, could you try with:
<TextBlock Text="{Binding boundProperty, StringFormat='Default value: {0}'}"/>

Yes, it works with the normal textbox text binding.

@Awsmolak this is actually by design with WPF.

Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.

The key is that last condition: "if it displays the bound value as a string". It is a bit of poor wording, but it basically means that the StringFormat property only applies if the Binding is being set on a string dependency property.
In this case the Hint property is of type object so the StringFormat property will never work if directly applied. This allows you to specify more than just strings for the hint (for example you could specify a StackPanel that contains a PackItem and TextBlock). @jespersh suggestion above is correct. If you want to use the StringFormat on the Hint property you need to specify the Hint as a TextBlock and bind you string to its Text property.

Something like this:

<TextBox>
    <materialDesign:HintAssist.Hint>
        <TextBlock Text="{Binding MyProperty, StringFormat='Foo: {0}'}" />
    </materialDesign:HintAssist.Hint>
</TextBox>
Was this page helpful?
0 / 5 - 0 ratings