xaml代码:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="260">
</ColumnDefinition>
<ColumnDefinition Width="400"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120">
</RowDefinition>
<RowDefinition Height="120"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.Column="1" x:Name="PreviewColor" Fill="#FF6600" Margin="10" Stroke="#666666" StrokeThickness="2"></Rectangle>
<StackPanel Grid.Row="1" Grid.Column="1">
<TextBlock FontSize="12">Color</TextBlock>
<TextBox x:Name="HexColor" Width="160" Height="30" Text="#FF6600" Margin="10,5" FontSize="11"></TextBox>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" VerticalAlignment="Center">
<TextBlock Text="Alpha" FontSize="12" Margin="10,15,0,0"></TextBlock>
<Slider x:Name="AlphaSlider" Margin="20,0,10,0" Maximum="255" Value="255" ValueChanged="AlphaSlider_ValueChanged"></Slider>
<TextBlock Text="Red" FontSize="12" Margin="10,15,0,0"></TextBlock>
<Slider x:Name="RedSlider" Margin="20,0,10,0" Maximum="255" Value="255" ValueChanged="RedSlider_ValueChanged"></Slider>
<TextBlock Text="Green" FontSize="12" Margin="10,15,0,0"></TextBlock>
<Slider x:Name="GreenSlider" Margin="20,0,10,0" Maximum="255" Value="102" ValueChanged="GreenSlider_ValueChanged"></Slider>
<TextBlock Text="Blue" FontSize="12" Margin="10,15,0,0"></TextBlock>
<Slider x:Name="BlueSlider" Margin="20,0,10,0" Maximum="255" Value="0" ValueChanged="BlueSlider_ValueChanged"></Slider>
</StackPanel>
</Grid>
xaml.cs代码:
#region 初始化界面
public GridColor()
{
InitializeComponent();
//重新注册AlphaSlider_ValueChanged
this.AlphaSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(AlphaSlider_ValueChanged);
//重新注册RedSlider_ValueChanged
this.RedSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(RedSlider_ValueChanged);
//重新注册RedSlider_ValueChanged
this.GreenSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(GreenSlider_ValueChanged);
//重新注册RedSlider_ValueChanged
this.BlueSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(BlueSlider_ValueChanged);
}
/// <summary>
/// 功能描述:调整调色器显示颜色,并显示调整后的颜色值
/// </summary>
private void mm_AdjustColor()
{
if (AlphaSlider==null||RedSlider==null||GreenSlider==null||BlueSlider==null)
{
return;
}
Color color = Color.FromArgb((byte)AlphaSlider.Value, (byte)RedSlider.Value, (byte)GreenSlider.Value, (byte)BlueSlider.Value);
PreviewColor.Fill = new SolidColorBrush(color);
HexColor.Text = color.ToString();
}
#endregion
#region 主界面操作
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
/// <summary>
/// 功能描述:改变红色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mm_AdjustColor();
}
/// <summary>
/// 功能描述:改变原色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AlphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mm_AdjustColor();
}
/// <summary>
/// 功能描述:改变绿色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GreenSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mm_AdjustColor();
}
/// <summary>
/// 功能描述:改变蓝色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BlueSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mm_AdjustColor();
}
#endregion