zoukankan      html  css  js  c++  java
  • WPF实现多值绑定特性以及多值转换

    WPF中的实现

    我们首先来看一下常规的绑定

    <Window    x:Class="WpfApplicationSample.MainWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="MainWindow"    Height="350"    Width="525">    <Grid>        <!--WPF 单值绑定-->        <TextBlock            Text="{Binding Title}"></TextBlock>      </Grid></Window>

    这个很简单,我们几乎不需要做任何解释

     

    接下来看一下WPF中如何进行多值绑定

    <Window    x:Class="WpfApplicationSample.MainWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="MainWindow"    Height="350"    Width="525">    <Grid>        <!--WPF 单值绑定-->        <TextBlock            Text="{Binding Title}"></TextBlock>         <!--WPF 多值绑定,结合StringFormat-->        <TextBlock>            <TextBlock.Text>                <MultiBinding                    StringFormat=" {0}-{1}">                    <Binding                        Path="Title"></Binding>                    <Binding                        Path="Time"></Binding>                </MultiBinding>            </TextBlock.Text>         </TextBlock>    </Grid></Window>

    这是第一种多值绑定方式,可以直接通过StringFormat格式化多个值,并最终显示在TextBlock中。这种做法,在很多时候,都够用了。

     

    但是,在某些时候,我们可能需要对这些多个值做复杂的处理,光用StringFormat满足不了要求,怎么办呢?

    是的,我们会联想到使用ValueConverter。在System.Windows.Data这个命名空间中,我们以前用过一个IValueConverter的接口对吧,那是针对单值绑定的。关于这个接口,更多信息,可以参考 http://msdn.microsoft.com/zh-cn/library/system.windows.data.ivalueconverter.aspx

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;namespace WpfApplicationSample{    /// <summary>    /// WPF单值绑定转换器    /// 作者:陈希章    /// </summary>    public class TitleConverter:IValueConverter    {        #region IValueConverter Members         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            //这里实现具体逻辑            throw new NotImplementedException();        }         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            //这里实现具体逻辑            throw new NotImplementedException();        }         #endregion    }}

     

    既然是这个思路,那么有没有多值转换器呢?答案是有的。请参考

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;namespace WpfApplicationSample{    /// <summary>    /// WPF单值绑定转换器    /// 作者:陈希章    /// </summary>    class MultiValueConverterSample:IMultiValueConverter    {        #region IMultiValueConverter Members         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            //这里实现具体逻辑,请注意第一个参数是一个数组,可以传递多个值            throw new NotImplementedException();        }         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)        {            throw new NotImplementedException();        }         #endregion    }}

     

    那么,如何在XAML中使用这个转换器呢?其实和单值转换器是一样的,请参考下面的语法

    <Window    x:Class="WpfApplicationSample.MainWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="MainWindow"    Height="350"    Width="525"    xmlns:local="clr-namespace:WpfApplicationSample">     <Window.Resources>        <local:MultiValueConverterSample            x:Key="cv"></local:MultiValueConverterSample>    </Window.Resources>    <Grid>        <!--WPF 单值绑定-->        <TextBlock            Text="{Binding Title}"></TextBlock>         <!--WPF 多值绑定,结合StringFormat-->        <TextBlock>            <TextBlock.Text>                <MultiBinding                    StringFormat=" {0}-{1}">                    <Binding                        Path="Title"></Binding>                    <Binding                        Path="Time"></Binding>                </MultiBinding>            </TextBlock.Text>         </TextBlock>         <!--WPF 多值绑定,结合Converter-->         <TextBlock>            <TextBlock.Text>                <MultiBinding                    Converter="{StaticResource cv}">                    <Binding                        Path="Title"></Binding>                    <Binding                        Path="Time"></Binding>                </MultiBinding>            </TextBlock.Text>        </TextBlock>     </Grid></Window>

    看起来很好理解,对吧?这是WPF中为我们默认就提供的功能,确实很方便。

    但是,这个特性(多值绑定)却没有在Silverlight中实现。

  • 相关阅读:
    python unittest学习4---跳过测试与预计的失败
    vue element-ui 使用 el-scrollbar监听滚动条滚动事件,处理el-tabs滚动到顶部header吸顶效果
    vue element-ui 复制文本到粘贴板
    VS Code 在HTML中生成随机文本内容
    git 添加多个远程仓库命令
    javascript——常用基础API部分方法、面试题集合
    清明时节,css3如何将网页变成灰色
    ffmpeg合并本地/线上、破解下载m3u8格式视频并转mp4格式命令
    javascript面试题
    javascript以下几种情况转换成布尔类型会得到false
  • 原文地址:https://www.cnblogs.com/wlming/p/5039361.html
Copyright © 2011-2022 走看看