-1
我想根據我的字符串的長度在正確的字體大小的文本塊中顯示字符串,所以我想也許通過計數字符串長度或字符,然後更新我的字體大小,但我不知道該怎麼辦在代碼....計數字符串字符mvvm
我想根據我的字符串的長度在正確的字體大小的文本塊中顯示字符串,所以我想也許通過計數字符串長度或字符,然後更新我的字體大小,但我不知道該怎麼辦在代碼....計數字符串字符mvvm
<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"));
}
}
您也可以使用Converter
這樣
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();
}
}
在視圖中,聲明在資源部分中的轉換器:
<local:TextFontSizeConverter x:Key="Converter"/>
然後,其綁定到TextBlock
<TextBlock Text="{Binding MyText, Mode=TwoWay}" FontSize="{Binding MyText, Mode=TwoWay, Converter={StaticResource Converter}}" />
通過該解決方案,可以隨時重新使用邏輯與任何TextBlock的。
綁定或屬性名稱錯誤。兩者都應引用相同的屬性('FontSize12'或'FontSize1') –
是修改答案! – Sajeetharan