2010-11-22 6 views
1

레이블의 배경색을 빨강, 파랑 또는 녹색을 나타내는 세 개의 슬라이드 막대에 바인딩하여 색상 선택 도구를 만들려고합니다.WPF - 3 개의 슬라이더에 배경색 바인딩?

이제 배경색을 단일 문자열에 바인딩하는 방법을 알았지 만 세 가지 컨트롤에 바인딩하는 방법은 무엇입니까?

답변

4

IMultiValueConverter 클래스를 만들고 MultiBinding을 사용해야합니다. ControlBackground 속성을 바인딩하려는 경우 Brush을 반환해야합니다. 순진 구현은 다음과 같이 보일 것이다 :

public class BrushConverter : IMultiValueConverter 
{ 
    public object Converter(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return new SolidColorBrush(
      Color.FromArgb(255, Convert.ToByte(values[0]), 
       Convert.ToByte(values[1]), Convert.ToByte(values[2]))); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     // this can be implemented fairly easily 
     throw new NotSupportedException(); 
    } 
} 

을 그리고 당신은과 같이 사용합니다 :

<UserControl.Resources> 
    <lcl:BrushConverter x:Key="brushConverter" /> 
</UserControl.Resources> 

<StackPanel Orientation="Vertical"> 
    <StackPanel.Background> 
     <MultiBinding Converter="{StaticResource brushConverter}"> 
      <Binding Path="Value" ElementName="r" /> 
      <Binding Path="Value" ElementName="g" /> 
      <Binding Path="Value" ElementName="b" /> 
     </MultiBinding> 
    </StackPanel.Background> 
    <Slider x:Name="r" Minimum="0" Maximum="255" IsSnapToTickEnabled="True" /> 
    <Slider x:Name="g" Minimum="0" Maximum="255" IsSnapToTickEnabled="True" /> 
    <Slider x:Name="b" Minimum="0" Maximum="255" IsSnapToTickEnabled="True" /> 
</StackPanel> 
+0

5,. 고맙습니다. –

0

MultiBinding을 사용해야합니다. 3 개의 정수 (빨강, 녹색, 파랑)를 Color으로 변환하는 변환기를 작성해야합니다.

0

내가 최근에 사용하여 연결된 속성을 해결

Heres는 전체 코드

public static class BrushExtender 
{ 
    public readonly static DependencyProperty BrushProperty = 
     DependencyProperty.RegisterAttached("Brush", typeof(Brush), 
     typeof(BrushExtender), new PropertyMetadata(Brushes.Black, DoBrushChanged)); 

    public readonly static DependencyProperty RedChannelProperty = 
     DependencyProperty.RegisterAttached("RedChannel", typeof(int), 
     typeof(BrushExtender), new PropertyMetadata(DoColorChangedRed)); 

    public readonly static DependencyProperty GreenChannelProperty = 
     DependencyProperty.RegisterAttached("GreenChannel", typeof(int), 
     typeof(BrushExtender), new PropertyMetadata(DoColorChangedGreen)); 

    public readonly static DependencyProperty BlueChannelProperty = 
     DependencyProperty.RegisterAttached("BlueChannel", typeof(int), 
     typeof(BrushExtender), new PropertyMetadata(DoColorChangedBlue)); 

    public readonly static DependencyProperty AlphaChannelProperty = 
     DependencyProperty.RegisterAttached("AlphaChannel", typeof(int), 
     typeof(BrushExtender), new PropertyMetadata(DoColorChangedAlpha)); 

    public readonly static DependencyProperty ColourValueProperty = 
     DependencyProperty.RegisterAttached("ColourValue", typeof(string), 
     typeof(BrushExtender), new PropertyMetadata(DoValueChanged)); 

    public static void SetRedChannel(DependencyObject o, int value) 
    { 
     o.SetValue(RedChannelProperty, value); 
    } 

    public static void SetGreenChannel(DependencyObject o, int value) 
    { 
     o.SetValue(GreenChannelProperty, value); 
    } 

    public static void SetBlueChannel(DependencyObject o, int value) 
    { 
     o.SetValue(BlueChannelProperty, value); 
    } 

    public static void SetAlphaChannel(DependencyObject o, int value) 
    { 
     o.SetValue(AlphaChannelProperty, value); 
    } 

    public static void SetBrush(DependencyObject o, SolidColorBrush brush) 
    { 
     o.SetValue(BrushProperty, brush); 
    } 

    public static void SetColourValue(DependencyObject o, string value) 
    { 
     o.SetValue(ColourValueProperty, value); 
    } 

    private static void DoColorChangedRed(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     DoColorChange(d, (int)e.NewValue, c => c.R,() => 
      Color.FromArgb(color.A, ((byte)(int)e.NewValue), color.G, color.B)); 
    } 

    private static void DoColorChangedGreen(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     DoColorChange(d, (int)e.NewValue, c => c.G,() => 
      Color.FromArgb(color.A, color.R, ((byte)(int)e.NewValue), color.B)); 
    } 

    private static void DoColorChangedBlue(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     DoColorChange(d, (int)e.NewValue, c => c.B,() => 
      Color.FromArgb(color.A, color.R, color.G, (byte)(int)e.NewValue)); 
    } 

    private static void DoColorChangedAlpha(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     DoColorChange(d, (int)e.NewValue, c => c.A,() => 
      Color.FromArgb((byte)(int)e.NewValue, color.R, color.G, color.B)); 
    } 

    private static void DoColorChange(DependencyObject d, int newValue, Func<Color, int> colorCompare, 
     Func<Color> getColor) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     if (colorCompare(color) == newValue) 
      return; 
     var newBrush = new SolidColorBrush(getColor()); 
     d.SetValue(BrushProperty, newBrush); 
     d.SetValue(ColourValueProperty, newBrush.Color.ToString()); 
    } 

    private static void DoValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color; 
     if (color.ToString() == (string)e.NewValue) 
      return; 
     Color? newColour = null; 
     try 
     { 
      newColour = (Color) ColorConverter.ConvertFromString((string)e.NewValue); 
     } 
     catch { } 
     if (newColour == null) 
      return; 
     var newBrush = new SolidColorBrush(newColour.Value); 
     d.SetValue(BrushProperty, newBrush); 
    } 


    private static void DoBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     if (e.NewValue == e.OldValue) 
      return; 
     var colour = ((SolidColorBrush)e.NewValue).Color; 
     d.SetValue(RedChannelProperty, (int)colour.R); 
     d.SetValue(GreenChannelProperty, (int)colour.G); 
     d.SetValue(BlueChannelProperty, (int)colour.B); 
     d.SetValue(AlphaChannelProperty, (int)colour.A); 
     d.SetValue(ColourValueProperty, colour.ToString()); 
    } 
} 

그리고 XAML

속임수를 썼는지
<Window x:Class="ColourBlender.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ColourBlender="clr-namespace:ColourBlender" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

    <TextBlock Text="Red" /> 
    <TextBlock Text="Green" Grid.Row="1" /> 
    <TextBlock Text="Blue" Grid.Row="2" /> 
    <TextBlock Text="Alpha" Grid.Row="3" /> 

    <Slider Name="redSlider" Grid.Column="1" Minimum="0" Maximum="255" Width="200" Height="20" 
      Grid.ColumnSpan="2" Value="{Binding ElementName=rect, 
      Path=(ColourBlender:BrushExtender.RedChannel), Mode=TwoWay}" /> 
    <Slider Name="greenSlider" Grid.Column="1" Grid.Row="1" Minimum="0" Maximum="255" Width="200" Height="20" 
      Grid.ColumnSpan="2" Value="{Binding ElementName=rect, 
      Path=(ColourBlender:BrushExtender.GreenChannel), Mode=TwoWay}" /> 
    <Slider Name="blueSlider" Grid.Column="1" Grid.Row="2" Minimum="0" Maximum="255" Width="200" Height="20" 
      Grid.ColumnSpan="2" Value="{Binding ElementName=rect, 
      Path=(ColourBlender:BrushExtender.BlueChannel), Mode=TwoWay}" /> 
    <Slider Name="alphaSlider" Grid.Column="1" Grid.Row="3" Minimum="0" Maximum="255" Width="200" Height="20" 
      Grid.ColumnSpan="2" Value="{Binding ElementName=rect, 
      Path=(ColourBlender:BrushExtender.AlphaChannel), Mode=TwoWay}" /> 

    <Rectangle Fill="SandyBrown" Name="rect" Width="200" Height="50" Grid.Row="4" Grid.ColumnSpan="3" 
      Margin="0,20,0,10" ColourBlender:BrushExtender.Brush="{Binding RelativeSource={RelativeSource Self}, 
      Path=Fill, Mode=TwoWay}"/> 

    <TextBlock Text="Colour Value" Margin="5,0,5,0" Grid.Row="5" HorizontalAlignment="Center" /> 
    <TextBox Text="{Binding ElementName=rect, Path=(ColourBlender:BrushExtender.ColourValue), Mode=TwoWay}" 
      Margin="0,0,0,0" Grid.Row="5" Grid.Column="1" Width="100" HorizontalAlignment="Center" /> 

    <Button Content="Update" Grid.Row="5" Grid.Column="3" /> 
</Grid>