吴佳鑫的个人专栏

当日事当日毕,没有任何借口

导航

silverlight 转换器经典demo

当在 文本框中输入 大于0时,会显示向上的箭头,输入等于0时,会显示一条横线,输入小于0时,会显示向下的箭头。

 

 

前台XAML代码:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:local="clr-namespace:Class4Samples"
    x:Class="Class4Samples.BindingConverter"
    d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <local:NumberToArrowStyleConverter x:Key="NumberToArrowStyleConverter"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Horizontal" d:LayoutOverrides="Width" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="200,0,0,0">
            <TextBox x:Name="textBox" Text="-1" TextWrapping="Wrap" VerticalAlignment="Center"/>
            <TextBlock Text="%" TextWrapping="Wrap" VerticalAlignment="Center"/>
            <ContentControl Content="ContentControl" 
            Style="{Binding Text, ElementName=textBox, Mode=OneWay,
            Converter={StaticResource NumberToArrowStyleConverter}}"/>
        </StackPanel>
    </Grid>
</UserControl>

 

后台CS代码:

View Code
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Class4Samples
{
    public partial class BindingConverter : UserControl
    {
        public BindingConverter()
        {
            // Required to initialize variables
            InitializeComponent();
        }
    }
}

 

可以看到,后台一句代码也没有写。

我们用到了一个转换器。

 

代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Class4Samples
{
    public class NumberToArrowStyleConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double number;
            if (value != null && double.TryParse(value.ToString(), out number))
            {
                if (number > 0.0)
                {
                    return App.Current.Resources["UpStyle"];
                }
                else if (number < 0.0)
                {
                    return App.Current.Resources["DownStyle"];
                }
                else
                {
                    return App.Current.Resources["NormalStyle"];
                }
            }
            else
            {
                return null;
            }
        }

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

 

这样就可以实现上面说的需求了。

 

另外有一个技巧:我们写了一个转换器后,可以不用在XAML里面手工编写XAML代码去引用它。

我们可以在blend中,data面板中,

点击“从类中创建示例数据”,根据转换器名称搜索到我们要的转换器后,命名后,点击确定。就可以引到XAML中了。

 

要绑定时,在blend也是可以利用工具去做绑定。

 

posted on 2012-08-16 22:25  _eagle  阅读(784)  评论(0编辑  收藏  举报