jeudi 22 mai 2014

c# - comte chaîne caractère mvvm - Stack Overflow


I want to show string in textblock with the right fontsize according to length of my string, so i think maybe by counting string length or character and then updating my font size , but I have no idea how to do that in code....




    <Style x:Key="ApplicationNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding FontSize,Mode=TwoWay, Source={StaticResource Sampe}}"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="0,2,0,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Opacity="0.25" ShadowDepth="0"/>
</Setter.Value>
</Setter>
</Style>

Viewmodel.cs


public Double FontSize
{
get
{
return _fontSize;
}
set
{
_fontSize = value;
put your logic!
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("FontSize"));
}
}



You can also use Converter like this


public class TextFontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int size;
//value is MyText
//Your logic to calculate the font size;
...
return size;
}

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

In the view, declare the converter in the resources section:


<local:TextFontSizeConverter x:Key="Converter"/>

Then, bind the it to the TextBlock


<TextBlock Text="{Binding MyText, Mode=TwoWay}" FontSize="{Binding MyText, Mode=TwoWay, Converter={StaticResource Converter}}" />

With this solution, you can always re-use the logic with any TextBlock.



I want to show string in textblock with the right fontsize according to length of my string, so i think maybe by counting string length or character and then updating my font size , but I have no idea how to do that in code....



    <Style x:Key="ApplicationNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding FontSize,Mode=TwoWay, Source={StaticResource Sampe}}"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="0,2,0,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Opacity="0.25" ShadowDepth="0"/>
</Setter.Value>
</Setter>
</Style>

Viewmodel.cs


public Double FontSize
{
get
{
return _fontSize;
}
set
{
_fontSize = value;
put your logic!
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("FontSize"));
}
}


You can also use Converter like this


public class TextFontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int size;
//value is MyText
//Your logic to calculate the font size;
...
return size;
}

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

In the view, declare the converter in the resources section:


<local:TextFontSizeConverter x:Key="Converter"/>

Then, bind the it to the TextBlock


<TextBlock Text="{Binding MyText, Mode=TwoWay}" FontSize="{Binding MyText, Mode=TwoWay, Converter={StaticResource Converter}}" />

With this solution, you can always re-use the logic with any TextBlock.


0 commentaires:

Enregistrer un commentaire