【发布时间】:2016-10-02 03:45:26
【问题描述】:
我在数据模板中有一个文本块,我在其中显示通过绑定的数据。最初我需要在文本块中显示最多三行数据。如需更多数据,请点击查看更多扩展文本块的选项。
到此为止。我面临的主要问题是如果数据大小不超过三行,我就不必看到更多选项。
我如何知道我的数据只消耗 1 或 2 行文本块
提前致谢
【问题讨论】:
标签: c# wpf windows xaml windows-phone-8
我在数据模板中有一个文本块,我在其中显示通过绑定的数据。最初我需要在文本块中显示最多三行数据。如需更多数据,请点击查看更多扩展文本块的选项。
到此为止。我面临的主要问题是如果数据大小不超过三行,我就不必看到更多选项。
我如何知道我的数据只消耗 1 或 2 行文本块
提前致谢
【问题讨论】:
标签: c# wpf windows xaml windows-phone-8
假设您已将视图模型绑定到您的模板
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>
(记得之前把转换器作为静态资源)。
警告:未经测试,只是想法/提示。
【讨论】: