WPF——TargetNullValue(如何在绑定空值显示默认字符)
2021-04-13 05:28
                         标签:color   code   图片   width   private   block   value   str   pat    说明:在数据绑定时,如果有些字段为空值,那么在数据绑定时可以用默认值来显示为空的字段。       WPF——TargetNullValue(如何在绑定空值显示默认字符) 标签:color   code   图片   width   private   block   value   str   pat    原文地址:https://www.cnblogs.com/lonelyxmas/p/8986716.html 1  Grid>
 2         ListBox x:Name="lstPeople" Width="400">
 3             ListBox.ItemTemplate>
 4                 DataTemplate>
 5                     StackPanel Orientation="Horizontal">
 6                         TextBlock Text="{Binding Path=FirstName}" />
 7                         TextBlock Text=" " />
 8                         TextBlock Text="{Binding LastName}" />
 9                         TextBlock Text=", " />
10                         TextBlock Text="{Binding Age, TargetNullValue=‘Age Unknown‘}" />
11                     StackPanel>
12                 DataTemplate>
13             ListBox.ItemTemplate>
14         ListBox>
15     Grid> 1  public partial class MainWindow : Window
 2     {
 3         List
 4 
 5         public MainWindow()
 6         {
 7             InitializeComponent();
 8             lst = new List
 9             lst.Add(new Person() {FirstName="John", LastName="Doe", Age=34});
10             lst.Add(new Person() { FirstName = "Jane", LastName = "Doe" });
11             lst.Add(new Person() { FirstName = "Jack", LastName = "Frost", Age = 300 });
12         }
13 
14         private void Window_Loaded(object sender, RoutedEventArgs e)
15         {
16             lstPeople.ItemsSource = lst;
17         }
18     }
19 
20     public class Person
21     {
22         public string FirstName { get; set; }
23         public string LastName { get; set; }
24         public int? Age { get; set; }
25     }
上一篇:c# https请求
文章标题:WPF——TargetNullValue(如何在绑定空值显示默认字符)
文章链接:http://soscw.com/index.php/essay/75057.html