Binding对数据的转换和校验

Binding用于数据校验的属性是ValidationRules,用于数据转换的属性是Converet。

  • Binding的数据校验

Binding的ValidationRules属性是Collection<ValidationRule>,可以为每个Binding设置多个数据校验的条件,每个条件是一个ValidationRule类型对象。

Binding进行校验时默认的行为是认为来自Source的数据是没有问题的,只有来自Target的数据有问题(Target多为UI控件,所以等价于用户输入的数据)。所以只有Target到Source的数据才会进行校验。如果想要改变这种行为,就需要ValidatesOnTargetUpdated="True",不过建议写代码前想好校验的方向。

<Window x:Class="ValidationRulesSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:val="clr-namespace:ValidationRulesSample"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="txt" Width="100" Height="30">
            <TextBox.Text>
                <Binding Path="Value" ElementName="slider" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <val:RangeValidationRule ValidatesOnTargetUpdated="True"></val:RangeValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Slider x:Name="slider" Maximum="100" Minimum="-10" SmallChange="1" Width="300" Height="40">
            <!--<Slider.Value>
                <Binding Path="Text" ElementName="txt" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <val:RangeValidationRule ValidatesOnTargetUpdated="True"></val:RangeValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </Slider.Value>-->
        </Slider>
    </StackPanel>
</Window>
public class RangeValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            //throw new NotImplementedException();
            double d = 0;
            if (double.TryParse(value.ToString(), out d))
            {
                if (d >= 0 && d <= 100)
                {
                    return new ValidationResult(true, null);
                }
            }
            return new ValidationResult(false, "Validation False");
        }
    }

注释的地方就是无论Source到Target还是Target到Source的校验,不过显示错误的地方都是Slider,就是控件边框有红色的框,希望读者在此处多琢磨一下。

另外给出后台Binding的代码

            Binding binding = new Binding("Value") { Source = this.slider };
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            RangeValidationRule rvr = new RangeValidationRule();
            binding.ValidationRules.Add(rvr);
            this.txt.SetBinding(TextBox.TextProperty, binding);

当校验出错的时候,可以让ValidationResult对象携带一条出错信息,这需要用到路由事件,以后再详细介绍。

  • Binding的数据转换

先看刚才的例子

<StackPanel>
        <TextBox x:Name="txt" Width="100" Height="30">
            <!--<TextBox.Text>
                <Binding Path="Value" ElementName="slider" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <val:RangeValidationRule ValidatesOnTargetUpdated="True"></val:RangeValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>-->
        </TextBox>
        <Slider x:Name="slider" Maximum="100" Minimum="-10" SmallChange="1" Width="300" Height="40">
            <Slider.Value>
                <Binding Path="Text" ElementName="txt" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <val:RangeValidationRule ValidatesOnTargetUpdated="True"></val:RangeValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </Slider.Value>
        </Slider>
    </StackPanel>

Slider的Value是double类型,TextBox的Text是string类型,它们却能来去自如,这是Binding的数据转换机制Data Convert,因为double到string的转换比较简单,所以WPF自动为我们添加了数据转换器Data Converet。但有些类型之间的转换就不是WPF能替我们做的了,遇到这种情况我们只有自己写Converet了。方法就是创建一个类并让这个类实现IValueConverer接口,IValueConveret接口定义如下

public interface IValueConverter
    {
        // 摘要:
        //     转换值。
        //
        // 参数:
        //   value:
        //     绑定源生成的值。
        //
        //   targetType:
        //     绑定目标属性的类型。
        //
        //   parameter:
        //     要使用的转换器参数。
        //
        //   culture:
        //     要用在转换器中的区域性。
        //
        // 返回结果:
        //     转换后的值。如果该方法返回 null,则使用有效的 null 值。
        object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        //
        // 摘要:
        //     转换值。
        //
        // 参数:
        //   value:
        //     绑定目标生成的值。
        //
        //   targetType:
        //     要转换到的类型。
        //
        //   parameter:
        //     要使用的转换器参数。
        //
        //   culture:
        //     要用在转换器中的区域性。
        //
        // 返回结果:
        //     转换后的值。如果该方法返回 null,则使用有效的 null 值。
        object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    }

当Binding的Source流向Target,Convert方法将被调用

当Binding的Target流向Source,ConvertBack方法将被调用。

这两个方法的参数列表一模一样,第一个参数为Object,第二个参数用于确定方法返回的类型(注意避免与Binding的Target混淆),第三个参数用于把额外的信息传入方法,若需要传递多个信息则可把信息放入一个集合对象来传入方法。

Binding的Mode属性会影响这两个方法的调用

public enum BindingMode
    {
        // 摘要:
        //     导致对源属性或目标属性的更改可自动更新对方。此绑定类型适用于可编辑窗体或其他完全交互式 UI 方案。
        TwoWay = 0,
        //
        // 摘要:
        //     当绑定源(源)更改时,更新绑定目标(目标)属性。如果要绑定的控件为隐式只读控件,则适用此绑定类型。例如,可以绑定到如股市代号之类的源。或者,可能目标属性没有用于进行更改(例如表的数据绑定背景色)的控件接口。如果不需要监视目标属性的更改,则使用
        //     System.Windows.Data.BindingMode.OneWay 绑定模式可避免 System.Windows.Data.BindingMode.TwoWay
        //     绑定模式的系统开销。
        OneWay = 1,
        //
        // 摘要:
        //     当应用程序启动或数据上下文更改时,更新绑定目标。此绑定类型适用于以下情况:使用当前状态的快照适合使用的或数据状态实际为静态的数据。如果要从源属性初始化具有某个值的目标属性,并且事先不知道数据上下文,则也可以使用此绑定类型。实质上,这是
        //     System.Windows.Data.BindingMode.OneWay 绑定的较简单的形式,它在不更改源值的情况下可提供更好的性能。
        OneTime = 2,
        //
        // 摘要:
        //     当目标属性更改时更新源属性。
        OneWayToSource = 3,
        //
        // 摘要:
        //     使用绑定目标的默认 System.Windows.Data.Binding.Mode 值。每个依赖项属性的默认值都不同。一般情况下,用户可编辑控件属性(例如文本框和复选框的属性)默认为双向绑定,而多数其他属性默认为单向绑定。确定依赖项属性绑定在默认情况下是单向还是双向的编程方法是:使用
        //     System.Windows.DependencyProperty.GetMetadata(System.Type) 来获取属性的属性元数据,然后检查
        //     System.Windows.FrameworkPropertyMetadata.BindsTwoWayByDefault 属性的布尔值。
        Default = 4,
    }

如果Mode为TwoWay则两个方法都有可能被调用,如果Mode为OneWay,那么只有Convert方法会被调用。

请看例子

public enum Category
    {
        Bomber,
        Fighter
    }

    public enum State
    {
        Available,
        Locked,
        Unknown
    }

    public class Plane
    {
        public Category Category { get; set; }
        public string Name { get; set; }
        public State State { get; set; }
    }
class CategoryToSourceConveret : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Category c = (Category)value;
            switch (c)
            {
                case Category.Bomber:
                    return @"\Icons\Bomb.png";
                case Category.Fighter:
                    return @"\Icons\Fighter.png";
                default: return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
class StateToNullableBoolConveret : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            State s = (State)value;
            switch (s)
            {
                case State.Available:
                    return true;
                case State.Locked:
                    return false;
                case State.Unknown:
                default: return null;
            }
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? nb = (bool?)value;
            switch (nb)
            {
                case true:
                    return State.Available;
                case false:
                    return State.Locked;
                case null:
                default:
                    return State.Unknown;
            }
        }
    }
<Window x:Class="ConveretSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ConveretSample"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CategoryToSourceConveret x:Key="cts"/>
        <local:StateToNullableBoolConveret x:Key="stc"/>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="listBoxPlane" Height="160" Margin="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="20" Height="20" Source="{Binding Path=Category,Converter={StaticResource cts}}"></Image>
                        <TextBlock Text="{Binding Path=Name}" Width="60" Margin="80,0"></TextBlock>
                        <CheckBox IsThreeState="True" IsChecked="{Binding Path=State,Converter={StaticResource stc}}"></CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0" Click="buttonLoad_Click"></Button>
        <Button x:Name="buttonSave" Content="Save" Height="25" Margin="5,0" Click="buttonSave_Click"></Button>
    </StackPanel> 
</Window>

该例子完成了State类型到bool类型的转换,Categroy到string类型的转换。注意转换的方向。

原文地址:https://www.cnblogs.com/HelloMyWorld/p/2917841.html