为了得到更人性化的外观,需要设计如何修剪数据列表和数据字段。 为了得到更人性化的外观,需要设计如何修剪数据列表和数据字段。
数据转换
在基本绑定中,信息从源到目标传递过程没有任何变化。但有时候希望将信息转换到更友好的内容再呈现到界面上。WPF提供了两个工具:
- 字符串格式化
- 值转换器
单个属性
Binding.StringFormat 属性针对简单的,标准的格式化数字和日期而创建的。
<TextBox Text="{Binding Path=UnitCost, StringFormat={}{0:C}}"/> <TextBox Text="{Binding Path=UnitCost, StringFormat=The value is {0:C}.}"/> <ListBox DisplayMemberPath="UnitCost" ItemStringFormat="{0:C}"/> 值转换器功能更强大,创建值转换器需要4个步骤:
- 创建一个实现了 IValueConverter 接口的类
- 为该类声明添加 ValueConversion 特性,并指定目标数据类型
- 实现 Convert() 方法
- 实现 ConvertBack() 方法
[ValueConversion(typeof(decimal), typeof(string))] public class PriceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { decimal price = (decimal)value; return price.ToString("C", culture); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string price = value.ToString(cultere); decimal result; if(Decimal.TryParse(price, NumberStyles.Any, cultere, out result)) return result; return value; } } <!--在Resources中创建转换器对象,可以用于多个绑定--> <Window.Resources> <local:PriceConverter x:Key="PriceConverter"/> </Window.Resources> <TextBox Text="{Binding Path=UnitCost, Converter={StaticResource PriceConverter}}"/> 多个属性
<TextBlock> <TextBlock.Text> <!--使用 MultiBinding 替换 Binding--> <MultiBinding StringFromat="{1}, {0}"> <Binding Path="FirstName"/> <Binding Path="LastName"/> </MultiBinding> </TextBlcok.Text> </TextBlock> 如果希望完成更复杂的工作,需要使用值转换器:
<TextBox> <TextBox.Text> <MultiBinding Converter="{StaticResource ValueInStockConverter}"> <Binding Path="UnitCost"/> <Binding Path="UnitsInStock"/> </MultiBinding> </TextBox.Text> </TextBox> public class VallueInStockConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { decimal unitCost = (decimal)values[0]; int unitsInStock = (int)value[1]; return unitCost * unitsInStock; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } 列表控件
ItemsControl 类为封装列表中的控件定义了基本功能,所有列表控件都继承自该类。
| 属性名 | 说明 |
|---|---|
| ItemsSource | 数据源 |
| DisplayMemberPath | 期望数据项显示的属性 (更复杂的显示使用ItemTemplate) |
| ItemStringFormat | 为每个项格式化文本 |
| ItemContainerStyle | 通过样式可以设置封装每个项的容器的多个属性。自动创建这些封装器对象 |
| ItemContainerStyleSelector | 为每项的封装器选择样式的StyleSelector对象 |
| AIternationCount | 在数据中设置的交替集合数量 |
| ItemTemplate | 模板从绑定的对象提取合适的数据并安排到合适的控件组合中 |
| ItemTemplateSelector | 为每个项选择模板的 DataTemplateSelector 对象 |
| ItemsPanel | 用于包含列表中项的面板,所有封装器都添加到这个容器中 |
| GroupStyle | 定义应当如何格式化每个分组 |
| GroupStyleSelector | 为每个分组选择样式的 StyleSelector 对象 |
列表样式
ItemContainerStyle
当创建列表项时,列表控件会将其向下传递 ItemContainerStyle 属性,每个列表项都将应用该样式。
<ListBox Name="lstProducts" Margin="5" DisplayMemberPath="ModelName"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="LightSteelBlue"/> <Setter Property="Margin" Value="5"/> <Setter Property="Padding" Value="5"/> <!--触发器使得样式更加精彩--> <style.Triggers> <Trigger Property="IsSelected" V.............原文转载:http://www.shaoqun.com/a/1450744.html
顺义中家鑫园温泉在哪里?怎么走:http://www.30bags.com/a/658396.html
深圳光明欢乐田园圳园路开放了吗:http://www.30bags.com/a/729408.html
北京世园公园门票2022年1月价格:http://www.30bags.com/a/912416.html
飞特:https://www.ikjzd.com/w/2273
bonanza:https://www.ikjzd.com/w/275.html
ad公司:https://www.ikjzd.com/w/1332
邮件回复注意事项And节日营销技巧!:http://www.kjdsnews.com/a/761427.html
独立站选Ueeshop,Ueeshop B2C版本又该如何选择?:https://m.ikjzd.com/articles/153718
客户被繁琐的流程劝退?Ueeshop询盘购物车帮你挽回!:https://m.ikjzd.com/articles/153714
没有评论:
发表评论
注意:只有此博客的成员才能发布评论。