【问题标题】:Get line count in textblock- windows phone 8在 textblock-windows phone 8 中获取行数
【发布时间】:2016-10-02 03:45:26
【问题描述】:

我在数据模板中有一个文本块,我在其中显示通过绑定的数据。最初我需要在文本块中显示最多三行数据。如需更多数据,请点击查看更多扩展文本块的选项

到此为止。我面临的主要问题是如果数据大小不超过三行,我就不必看到更多选项

我如何知道我的数据只消耗 1 或 2 行文本块

提前致谢

【问题讨论】:

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


    【解决方案1】:

    假设您已将视图模型绑定到您的模板

    public class ViewModel : INotifyPropertyChanged
    {
         public string MyBoundText { get { .. }; set { .. }; }
    ]
    

    您可以只创建另一个属性:

    public int LinesNo => this.MyBoundText.Split('\n').Length; 
    //remember about null-check and other edge-cases
    

    我假设您的 xaml 中有某处:

    <TextBox Text="{Binding MyBoundText}"></TextBox>
    

    所以,创建自己的转换器:

    class Button1VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targettype, object parameter, System.Globalization.CultureInfo culture)
        {
            int mode = (int)value;
            if (mode <= (int)parameter)
                return System.Windows.Visibility.Collapsed;
            else
                return System.Windows.Visibility.Visible;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    

    然后将其与您的按钮绑定:

    <Button Content="Show more...">
        <Button.Visibillity>
            <Binding Path="LinesNo "
                     Converter="{StaticResource Button1VisibilityConverter }">
                <Binding.ConverterParameter>
                    <sys:Int32>3</sys:Int32>
                </Binding.ConverterParameter>
            </Binding>
        </Button.Visibillity>
    </Button>
    

    (记得之前把转换器作为静态资源)。

    警告:未经测试,只是想法/提示。

    【讨论】:

    • 谢谢。但我认为你是在“/n”的基础上计算行数。但我的文本中没有收到任何“/n”字符。我需要在文本块中换行的基础上计算行数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多