十年河东,十年河西,莫欺少年穷
学无止境,精益求精
行为请参考:WPF 行为
装饰器参考:
wpf 转换器
详情参考:
单值转换器需继承自 IValueConverter
public class MyNumberConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value!=null&& System.Convert.ToInt32(value) % 2 == 0)
{
return "偶数";
}
return "奇数";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
多值转换器需继承自IMultiValueConverter
public class MyColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Length > 2)
{
var R = System.Convert.ToByte(values[0]);
var G = System.Convert.ToByte(values[1]);
var B = System.Convert.ToByte(values[2]);
System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb(R,G,B);
return new SolidColorBrush(color);
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
使用转换器
xaml 如下:
<Window x:Class="WpfApp9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:MyConverter="clr-namespace:WpfApp9.Converter"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
<Window.Resources>
<MyConverter:MyNumberConverter x:Key="MyNumber"/>
<MyConverter:MyColorConverter x:Key="MyColor"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBox x:Name="txt" Text="0" Width="100" Height="40" FontFamily="50"/>
<TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" />
</StackPanel>
<StackPanel Grid.Column="1">
<Slider x:Name="Rcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
<Slider x:Name="Gcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
<Slider x:Name="Bcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
<Path VerticalAlignment="Center" Stroke="Black" StrokeThickness="3">
<Path.Data>
<EllipseGeometry Center="80,80" RadiusX="60" RadiusY="60"/>
</Path.Data>
<Path.Fill>
<MultiBinding Converter="{StaticResource MyColor}">
<Binding ElementName="Rcolor" Path="Value"></Binding>
<Binding ElementName="Gcolor" Path="Value"></Binding>
<Binding ElementName="Bcolor" Path="Value"></Binding>
</MultiBinding>
</Path.Fill>
</Path>
</StackPanel>
</Grid>
</Window>
View Code
效果:
注:需要在资源中引入定义的转换器
<Window.Resources>
<MyConverter:MyNumberConverter x:Key="MyNumber"/>
<MyConverter:MyColorConverter x:Key="MyColor"/>
</Window.Resources>
使用如下:
<TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" />