question

MERUNKUMARMAITY-4120 avatar image
0 Votes"
MERUNKUMARMAITY-4120 asked Viorel-1 commented

Why IValueConverter does not return a value?

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.

dotnet-csharpwindows-wpfdotnet-wpf-xaml
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you also define the FontSize property, which is specified in binding path?


1 Vote 1 ·

Thank you sir for your reply. But I can not understand what you want say by defining Font Size property in binding path? I already define Font Size =10 in the button, I have to find a way to give this in Text block, that's why I use a IvlaueConverter to return a double value of 10. If you know any other way then please help me.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 MERUNKUMARMAITY-4120 ·

According to your XAML, I think that the FontSize of textbox is got from FontSize property of your application class, then it is converted. Therefore, your application class (usually App.xaml.cs) is expected to contain a property like this:

public double FontSize { get; set; } = 10.0;

You can also consider a “dependency property”.

If there is no such property, then the converter is not achieved, I think.

Maybe the converter is not needed if you set this property programmatically.


0 Votes 0 ·

0 Answers