深入浅出WPF 第二部分(11)

本文介绍了一个WPF应用程序中使用的两种数据绑定转换器:CategoryToImageConverter用于将飞机类别转换为图片路径;StateToNullableBooleanConverter用于将飞机状态枚举转换为布尔值或空值。这两种转换器实现了IValueConverter接口,分别用于UI元素如图像源和复选框的状态显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6.4.2 Binding的数据转换

当数据从Binding的Source流向Target时,Convert方法将被调用;反之,ConvertBack方法将被调用。

using System;
using System.Windows.Data;

using FirstWpfApplication.Objects;

namespace FirstWpfApplication.Converters
{
    class CategoryToImageCoverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Plane.CategoryEnum ct = (Plane.CategoryEnum)value;
            switch (ct)
            {
                case(Plane.CategoryEnum.Bomber):
                    return @"\Images\bomber.png";
                case (Plane.CategoryEnum.Fighter):
                    return @"\Images\fighter.png";
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

using System;
using System.Windows.Data;

using FirstWpfApplication.Objects;

namespace FirstWpfApplication.Converters
{
    class StateToNullableBooleanCoverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Plane.StateEnum st = (Plane.StateEnum)value;
            switch (st)
            {
                case Plane.StateEnum.Available:
                    return true;
                case Plane.StateEnum.Locked:
                    return false;
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool? state = value as bool?;
            if (state == null)
                return Plane.StateEnum.UnKnown;
            else if (state.Value)
                return Plane.StateEnum.Available;
            else
                return Plane.StateEnum.Locked;
        }
    }
}

namespace FirstWpfApplication.Objects
{
    class Plane
    {
        public string Name { get; set; }
        public CategoryEnum Category { get; set; }
        public StateEnum State { get; set; }

        public enum CategoryEnum
        {
            Bomber,
            Fighter
        }

        public enum StateEnum
        {
            Available,
            Locked,
            UnKnown
        }
    }
}

<Window x:Class="FirstWpfApplication.MainWindow"
        x:ClassModifier="public"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
        xmlns:local="clr-namespace:FirstWpfApplication"
        xmlns:converter="clr-namespace:FirstWpfApplication.Converters"
        Title="登录" Height="250" Width="200" >
    <Window.Resources>
        <converter:CategoryToImageCoverter x:Key="ctic"/>
        <converter:StateToNullableBooleanCoverter x:Key="stnbc"/>
    </Window.Resources>

    <Grid x:Name="g1" Background="Orange" Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="120" />
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <ListBox x:Name="planeListBox" Grid.Row="0" Margin="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <!--[ˌɔ:rienˈteiʃən] 方向 定位-->
                        <Image Width="40" Height="20" Source="{Binding Path=Category, Converter={StaticResource ResourceKey=ctic}}"/>
                        <TextBlock Width="80" Height="20" Text="{Binding Path=Name}"/>
                        <CheckBox Width="40" Height="20" IsThreeState="True" IsChecked="{Binding Path=State, Converter={StaticResource ResourceKey=stnbc}}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Load" x:Name="buttonLoad" Grid.Row="1" Width="60" Height="25" Click="buttonLoad_Click"/>
        <Button Content="Save" x:Name="buttonSave" Grid.Row="2" Width="60" Height="25" Click="buttonSave_Click"/>
    </Grid>
</Window>

        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中.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值