您的位置:首页 > 其它

WPF 自定义文本列表

2015-08-24 12:35 190 查看
自定义的WPF文本列表样式



一、直接把样式贴出来

<!--文本列表样式-->
<Style x:Key="TextItemsListStyle" TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="4" Height="4" Margin="6,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Fill="Gray"></Ellipse>
<Label Height="25" Content="{Binding Visual}" ToolTip="{Binding Title}" Foreground="#659BFF"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>


二、如何使用

<ItemsControl Style="{StaticResource TextItemsListStyle}" Name="ItemList"/>

this.ItemList.ItemsSource = new List<object>()
{
new { Title = "第1行文本提示",Visual = "第1行文本内容"},
new { Title = "第2行文本提示",Visual = "第2行文本内容"},
new { Title = "第3行文本提示",Visual = "第3行文本内容"}
};


三、如何添加省略号。。。

StringSupplement(YourTitle,32,"...")

public string StringSupplement(string OldString,int MaxLeght,string EndString = "")
{
string NewString = OldString;
int EndLength = 0;
for (int nIndex = 0; nIndex < EndString.Length; nIndex++)
{
byte[] byte_len = System.Text.Encoding.Default.GetBytes(EndString.Substring(nIndex, 1));
if (byte_len.Length > 1)
EndLength += 2;  //如果长度大于1,是中文,占两个字节,+2
else
EndLength += 1;  //如果长度等于1,是英文,占一个字节,+1
}
int Length = 0;
for (int nIndex = 0; nIndex < OldString.Length; nIndex++)
{
byte[] byte_len = System.Text.Encoding.Default.GetBytes(OldString.Substring(nIndex, 1));
//如果长度大于1,是中文,占两个字节,+2
//如果长度等于1,是英文,占一个字节,+1
Length += byte_len.Length > 1 ? 2 : 1;
//如果长度超出添加结束字符串
if (Length + EndLength > MaxLeght)
{
NewString = OldString.Substring(0, nIndex) + EndString;
break;
}
}
return NewString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: