【问题标题】:Count string character mvvm [closed]计算字符串字符 mvvm [关闭]
【发布时间】:2014-03-19 04:41:38
【问题描述】:

我想根据我的字符串长度在文本块中显示具有正确字体大小的字符串,所以我想可能是通过计算字符串长度或字符然后更新我的字体大小,但我不知道如何在代码中做到这一点。 ...

【问题讨论】:

    标签: c# xaml windows-phone-7 windows-phone-8


    【解决方案1】:
        <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"));
        }
    }
    

    【讨论】:

    • 要么绑定错误,要么属性名错误。两者都应该引用相同的属性(FontSize12FontSize1
    • 是修改了答案!
    【解决方案2】:

    你也可以像这样使用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 重复使用逻辑。

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多