背景:實現Slider拖動可以調整rgb
單轉換器:WPF中數據綁定轉換器Converter-CSDN博客
在View中:
<StackPanel Orientation="Vertical">
<Slider x:Name="slider_R" Minimum="0" Maximum="255" Width="200" Margin="20"/>
<Slider x:Name="slider_G" Minimum="0" Maximum="255" Width="200" Margin="20"/>
<Slider x:Name="slider_B" Minimum="0" Maximum="255" Width="200" Margin="20"/>
<Path VerticalAlignment="Center" HorizontalAlignment="Center">
<Path.Data>
<EllipseGeometry Center="50, 50" RadiusX="50" RadiusY="50"/>
</Path.Data>
<Path.Fill>
<MultiBinding Converter="{StaticResource rmc}">
<Binding ElementName="slider_R" Path="Value"/>
<Binding ElementName="slider_G" Path="Value"/>
<Binding ElementName="slider_B" Path="Value"/>
</MultiBinding>
</Path.Fill>
</Path>
</StackPanel>
? ? ? ? -- Fill中綁定的資源是Window.Resources中引入進來的轉換器key
? ? ? ? -- 轉換器返回的是一個SolidBrush,其實就是相當于在這個地方寫個SolidBrush
? ? ? ? -- 這里綁定的是Slider的值,但是如果是有值的就直接Path指向那個值就行:
????????????????
<Button.Style>
<!--單值轉換器-->
<!--<Style TargetType="Button">
<Setter Property="IsEnabled" Value="{Binding Age, Converter={StaticResource singleConverter}}"/>
</Style>-->
<Style TargetType="Button">
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{StaticResource multiConverter}">
<Binding Path="Age"/>
<Binding Path="Grade"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
轉換器中:文章來源:http://www.zghlxwxcb.cn/news/detail-817235.html
public class rgbMultiConventer : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values==null || values.Length < 2)
{
return null;
}
byte r = System.Convert.ToByte(values[0]);
byte g = System.Convert.ToByte(values[1]);
byte b = System.Convert.ToByte(values[2]);
Color color = Color.FromRgb(r, g, b);
return new SolidColorBrush(color);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
?-- 就是轉換器類接上接口“IMultiValueConverter”文章來源地址http://www.zghlxwxcb.cn/news/detail-817235.html
到了這里,關于WPF多值轉換器的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!