簡體   English   中英

獲取文本塊中的行數-Windows Phone 8

[英]Get line count in textblock- windows phone 8

在數據模板中有一個文本塊, 通過綁定在其中顯示數據 最初,我需要在文本塊中顯示多達三行的數據。 有關更多數據, 請點擊查看更多選項,以擴展文本塊

至此完成。 我面臨的主要問題是, 如果數據大小不超過三行,我不必費心看到更多選擇

我如何知道我的數據僅占用1或2行文本塊

提前致謝

考慮您已將視圖模型綁定到模板

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

我假設您的xml中包含以下內容:

<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>

(切記將轉換器放置為靜態資源)。

警告:未經測試,僅是想法/提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM