不久之前用到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>