深入浅出WPF 第二部分(11)
6.4.2 Binding的数据转换
当数据从Binding的Source流向Target时,Convert方法将被调用;反之,ConvertBack方法将被调用。
private void buttonLoad_Click(object sender, RoutedEventArgs e) { List<Plane> planes = new List<Plane>() { new Plane(){Name="J-10", Category=Plane.CategoryEnum.Fighter, State=Plane.StateEnum.Available}, new Plane(){Name="H-6", Category=Plane.CategoryEnum.Bomber, State=Plane.StateEnum.Available}, new Plane(){Name="F-22", Category=Plane.CategoryEnum.Fighter, State=Plane.StateEnum.Locked}, new Plane(){Name="B-2", Category=Plane.CategoryEnum.Bomber, State=Plane.StateEnum.UnKnown} }; this.planeListBox.ItemsSource = planes; } private void buttonSave_Click(object sender, RoutedEventArgs e) { StringBuilder sb = new StringBuilder(); foreach (Plane p in planeListBox.Items) sb.AppendLine(string.Format("Category={0},Name={1},State={2}", p.Category, p.Name, p.State)); File.WriteAllText(@"D:\PlaneList.txt", sb.ToString()); }
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an excep
产生这个错误的原因是,StaticResource必须先定义再引用,但是DynamicResource就没有这个限制,为避免这个错误出现,可将StaticResource的定义放在Window.xaml的最前端,或者放在App.xaml中.