Question

Dynamic Sizing FontSize binding error

Posted 2 years ago by Avatar Miles Merckel

Hi

I’ve created a dynamically sized gauge UserControl and using the dynamically sized TextBlock within the gauge same as in the Dynamic Sizing example. The control works as intended but there is an annoying binding error I can’t get rid of.

Error

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='0' BindingExpression:Path=UnitBaseY; DataItem='CircularGauge' (Name='myGauge'); target element is 'TextBlock' (Name=''); target property is 'FontSize' (type 'Double')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='0' BindingExpression:Path=UnitBaseY; DataItem='CircularGauge' (Name='myGauge'); target element is 'TextBlock' (Name=''); target property is 'FontSize' (type 'Double')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='0' BindingExpression:Path=UnitBaseY; DataItem='CircularGauge' (Name='myGauge'); target element is 'TextBlock' (Name=''); target property is 'FontSize' (type 'Double')

 Code

<UserControl x:Class="FontTest.VU_ResizableCurrent"
             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" 
             xmlns:gauge="http://schemas.actiprosoftware.com/winfx/xaml/gauge"
             xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
             xmlns:Comps="clr-namespace:FontTest"
             mc:Ignorable="d" 
             d:DesignHeight="180" d:DesignWidth="190" >
    
    <UserControl.Resources>
        <shared:MultiplicationConverter x:Key="MultiplicationConverter" />
    </UserControl.Resources>

    <Grid>
        <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="White" />
        <shared:ResizableContentControl>
            <gauge:CircularGauge x:Name="myGauge" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="1" FrameType="None" >
                <gauge:CircularGauge.Items>
                    <TextBlock gauge:CircularGaugeBase.Y="18%" gauge:CircularGaugeBase.X="0%" Text="A" Foreground="Black" FontSize="{Binding UnitBaseY, ElementName=myGauge, Converter={StaticResource MultiplicationConverter}, ConverterParameter=0.15}" FontWeight="Bold"/>
                    <TextBlock gauge:CircularGauge.Y="-10%" Text="Resizable" Foreground="Green" FontSize="{Binding UnitBaseY, ElementName=myGauge, Converter={StaticResource MultiplicationConverter}, ConverterParameter=0.15}" />
                    <TextBlock gauge:CircularGauge.Y="10%" Text="Gauge" Foreground="Blue" FontSize="{Binding UnitBaseY, ElementName=myGauge, Converter={StaticResource MultiplicationConverter}, ConverterParameter=0.15}" />
                </gauge:CircularGauge.Items>
            </gauge:CircularGauge>
        </shared:ResizableContentControl>
    </Grid>
</UserControl>

 Is there something I’m missing or is it possible to mute the Error?

Many Thanks

Miles


Comments (3)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Miles,

The problem here seems to be that the initial result of the binding is 0 (zero) due to UnitBaseY being zero originally, and that is being set to the FontSize property.  Zero is an invalid value for the FontSize property.  If you inherit the MultiplicationConverter class and have its Convert method ensure that the value returned is greater than zero (perhaps do a Math.Max(1.0, baseValue)), then that should remove this binding error.

UPDATE: Changed Math.Min to Max above.

[Modified 2 years ago]


Actipro Software Support
Posted 2 years ago by Miles Merckel

Thanks for the reply. I'm actually seeing the binding error in the original Actipro demo now, may be didn't see it before as it only fires when the specific example is activated.

 

The Y value for all textblocks is set in the markup but that still gives a zero initially? Wouldn't the Math.Max be more appropriate as Math.Min(1.0, baseValue)) would still give 0 if baseValue was 0.

 

What would the inherited MultiplicationConverter code be?

 

Many Thanks

Miles

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Miles,

Sorry that was a typo, it should have been Math.Max above.  They are binding to the UnitBaseY property, which I believe is zero initially before it does a layout and changes to something larger.

For the inherited MultiplicationConverter class, just override Convert, call the base method to get the 'baseValue' result, and then cast that to a double and do the Math.Max(1.0, baseValue) as its result.  That should ensure it's at least a font size of 1.0.


Actipro Software Support
Information The latest build of this product (2018.1 build 0670) was released 4 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.