Why IValueConverter does not return a value?

MERUN KUMAR MAITY 531 Reputation points
2021-08-03T20:10:22.927+00:00

I use WPF Fluid layout. I have a button and I use a Text block as a button content. But I also use a view box there to scale my text. I want the text of the text block is perfectly visible in every screen resolution that's why I use a data binding with application current state. And all of my problem is solved that means there no text blurry issue at the runtime of the application at every screen resolution.

But one problem is I want to scale the text in FontSize=10, I can not declare the FontSize =10 because I do data binding there, so there no place to give the value in number. only one way to do that is using a Value Converter. By that method the Value Converter return a value in which the binding will scale the text.

Here is the code -

  <Button
        x:Name="BtnSettings" Width="90" HorizontalAlignment="Left" Margin="200,14,0,0" Height="30" DockPanel.Dock="Left" FontSize="10"
     VerticalAlignment="Top"  UseLayoutRounding="True"  RenderOptions.ClearTypeHint="Enabled"  RenderOptions.BitmapScalingMode="NearestNeighbor"   SnapsToDevicePixels="True"        

                               >
                    <Button.Content >
                        <Viewbox Height="15" SnapsToDevicePixels="True" StretchDirection="Both"   x:Name="myViewbox"  Stretch="Uniform"  HorizontalAlignment="Stretch"  >
                            <TextBlock x:Name="myTextbox" 

                                       SizeChanged="myTextbox_SizeChanged" 
                                       FontFamily="Segoe UI" UseLayoutRounding="True" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"  TextOptions.TextFormattingMode="Display"       Margin="0,-2,0,0" TextOptions.TextRenderingMode="ClearType"  RenderOptions.ClearTypeHint="Enabled"  FontSize="{Binding Source={x:Static Application.Current}, Path=FontSize, Mode=TwoWay, Converter={StaticResource FontSizeConverter} }">

                    Settings
                        </TextBlock>
                        </Viewbox>
                    </Button.Content>                   
                </Button>

And here is the part of the code of the button control template - (Because to maintain this data binding I also need to do another binding in button control template) -

 <Style
                x:Key="RoundCorner"
                x:Name="RoundCornerButton"
                TargetType="{x:Type Button}">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Padding" Value="1" />
            <Setter Property="Foreground" Value="#bababa" />
            <Setter Property="ToolTipService.InitialShowDelay" Value="500" />
            <Setter Property="ToolTipService.ShowDuration" Value="4000" />

            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize">
                <Setter.Value>
                    <Binding Source="{x:Static Application.Current}" Path="FontSize"/>
                </Setter.Value>
            </Setter>

And here is the code of the IValue Converter from which I want to get a number value so that I bind with the application current state at font size 10 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WpfApp1
{
   public class FontSizeConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return 10d;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

If you see the converter code very carefully then you see that I return 10d, because I want to scale at font size 10 and d here is for double data type. But unfortunately the code of the converter does not return the value.

My question is, is there any other way ? to achieve this ? or should I correct the converter code.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
790 questions
{count} votes