1.UI
<Window.Resources>
<!--普通转换器 -->
<local:DispalyConverter x:Key="dis"></local:DispalyConverter>
<!--多值转换器 -->
<local:MultDispalyConverter x:Key="multDis"></local:MultDispalyConverter>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel>
<!--普通转换器-->
<TextBox Text="0" Name="t1" FontSize="50"></TextBox>
<TextBlock Text="{Binding ElementName=t1,Path=Text,Converter={StaticResource dis}}" FontSize="50"></TextBlock>
</StackPanel>
<StackPanel Grid.Column="1">
<Slider x:Name="slider_R" Minimum="0" Maximum="255" Margin="10"></Slider>
<Slider x:Name="slider_G" Minimum="0" Maximum="255" Margin="10"></Slider>
<Slider x:Name="slider_B" Minimum="0" Maximum="255" Margin="10"></Slider>
<!--<TextBlock Margin="20" FontSize="50">
--><!--<TextBlock.Background>
--><!--多值转换器,将三个颜色转换为背景色--><!--
<MultiBinding Converter="{StaticResource multDis}">
<Binding ElementName="slider_R" Path="Value" />
<Binding ElementName="slider_G" Path="Value" />
<Binding ElementName="slider_B" Path="Value" />
</MultiBinding>
</TextBlock.Background>--><!--
<TextBlock.Text>
--><!--多值转换器,将三个颜色转换为背景色--><!--
<MultiBinding Converter="{StaticResource multDis}">
<Binding ElementName="slider_R" Path="Value" />
<Binding ElementName="slider_G" Path="Value" />
<Binding ElementName="slider_B" Path="Value" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>-->
<!--不知道为什么TextBlock.Text里面不能显示值,Label.Content可以显示出来-->
<Label Margin="20" FontSize="50">
<Label.Background>
<MultiBinding Converter="{StaticResource multDis}">
<Binding ElementName="slider_R" Path="Value" />
<Binding ElementName="slider_G" Path="Value" />
<Binding ElementName="slider_B" Path="Value" />
</MultiBinding>
</Label.Background>
<Label.Content>
<MultiBinding Converter="{StaticResource multDis}">
<Binding ElementName="slider_R" Path="Value" />
<Binding ElementName="slider_G" Path="Value" />
<Binding ElementName="slider_B" Path="Value" />
</MultiBinding>
</Label.Content>
</Label>
</StackPanel>
</Grid>
普通转换器,功能实现
public class DispalyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
string str = value.ToString();
if (str== "1")
{
return "True";
}
else
{
return "False";
}
}
return "False";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
多值通转换器,功能实现
public class MultDispalyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 3)
{ return null; }
byte r = System.Convert.ToByte(values[0]);
byte g = System.Convert.ToByte(values[1]);
byte b = System.Convert.ToByte(values[2]);
System.Windows.Media.Color color= System.Windows.Media.Color.FromRgb(r, g, b);
SolidColorBrush brush = new SolidColorBrush(color);
return brush;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Demo: