WPF之数值转换器IValueConverter Demo

不久之前用到WPF的IValueConverter,不太理解的用上了,最近多看了些相关知识,有了进一步了解,现在放一个demo以留备用。实现了值转换传递,Image控件属性绑定Image自身的其他属性和布尔量取反。

Xaml.cs文件

using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            A.IsChecked = true;
        }
    }
    public class WndRadio : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //From Height to Width
            if (value == null)
                return DependencyProperty.UnsetValue;
            return (double)value *(double)parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //From Width to Height
            return (double)value /(double)parameter;
        }
    }
    public class Reverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? false : true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? false : true;
        }
    }
}

Xaml文件

<Window x:Class="BindingTest.MainWindow"
        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"
        xmlns:local="clr-namespace:BindingTest"
        xmlns:system ="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="600">
    <Window.Resources>
        <local:WndRadio x:Key="wndRatio"/>
        <local:Reverter x:Key="reverter"/>
        <system:Double x:Key="v">
            1.77777777777777777
        </system:Double>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="353*"/>
            <ColumnDefinition Width="239*"/>
        </Grid.ColumnDefinitions>
        <Image x:Name="image" Width="{Binding Path=ActualHeight,Converter={StaticResource wndRatio}, ConverterParameter={StaticResource v}, RelativeSource={RelativeSource Self}}" Source="Screenshot.jpg" Stretch="Fill" Height="180" Margin="1,120,32,119"/>
        <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="97,111,0,0" TextWrapping="Wrap" Text="{Binding Path=Width,ElementName=image}" VerticalAlignment="Top" Width="120"/>
        <Slider Grid.Column="1" Width="120" Maximum="500" Value="{Binding ElementName=image,Path=Height}" HorizontalAlignment="Left" Margin="97,212,0,0" VerticalAlignment="Top"/>
        <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="97,156,0,0" TextWrapping="Wrap" Text="{Binding Height, ElementName=image}" VerticalAlignment="Top" Width="120"/>
        <CheckBox x:Name="A" Content="CheckBox" Grid.Column="1" HorizontalAlignment="Left" Margin="52,257,0,0" VerticalAlignment="Top"/>
        <CheckBox x:Name="B" IsChecked="{Binding Path=IsChecked,ElementName=A,Converter={StaticResource reverter}}" Grid.Column="1" HorizontalAlignment="Left" Margin="65,300,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值