我正在尋找WPF的顏色對話框? 有沒有內置的? 我應該建立自己的? 還是贏得32個互操作? 如果是這樣,怎麼樣?是否有WPF的顏色對話框?
13
A
回答
3
繼承人使用附加屬性,一個簡單的顏色選擇器應該有人來尋找示例代碼
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());
}
}
,在這裏它被用來
<Window x:Class="ChannelColourBrush.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:ChannelColourBrush" Title="Window1" Height="300" Width="300">
<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=(c: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=(c: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=(c: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=(c:BrushExtender.AlphaChannel), Mode=TwoWay}" />
<Rectangle Fill="SandyBrown" Name="rect" Width="200" Height="50" Grid.Row="4" Grid.ColumnSpan="3" Margin="0,20,0,10"
c: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=(c:BrushExtender.ColourValue), Mode=TwoWay}" Margin="0,0,0,0" Grid.Row="5" Grid.Column="1" Width="100" HorizontalAlignment="Center" />
<Button Content="Update" IsEnabled="{Binding ElementName=grid, Path=SelectedItem.SomeValue}"/>
</Grid>
4
我寫了一個簡單的WPF顏色選擇器支持以下功能一會兒回
- 3種不同類型的色樣
- 不透明度滑塊的
- 爲開放
- 標準對話按鈕
這裏選擇顏色
+0
這看起來非常好。如果我只能顯示色輪而不是所有其他控件,我都會使用它。 – Steven 2013-09-06 23:37:41
相關問題
- 1. 什麼是WPF對話框的良好默認背景顏色?
- 2. Android的對話框顏色
- 3. 顏色對話框沒有顯示
- 4. 如何使用WPF打開顏色和字體對話框?
- 5. 設置對話框的「暗淡」顏色
- 6. 如何改變對話框的顏色
- 7. WPF SystemColors:TextBox邊框的顏色
- 8. WPF組合框的顏色
- 9. Android ICS對話框標題顏色
- 10. PhpStorm輸入對話框顏色
- 11. 瞭解顏色選擇器對話框
- 12. Qt顏色選擇對話框,如gcolor2
- 13. Android對話框分隔線顏色
- 14. 從顏色對話框中禁用顏色
- 15. 是否可以在Applescript的對話框中設置文本的顏色?
- 16. 什麼是Theme.Holo.Dialog的對話框背景顏色?
- 17. 是否有「筆」類來補充WPF中的「筆刷」和「顏色」?
- 18. CDialog的背景顏色和該對話框中的所有控件都是白色而不是灰色
- 19. 檢測是否有對話框打開
- 20. neo4j GRASS邊框顏色是否屬性?
- 21. WPF xaml綁定組合框背景顏色爲SelectedItem的顏色
- 22. 對話框的邊框和分隔線顏色
- 23. 更改對話框微調框的popupbackground顏色
- 24. 更改Android中對話框標題周圍框架的顏色
- 25. 如何更改對話框的邊框顏色?
- 26. WPF的ScrollViewer對話框
- 27. 在android vars或常量中是否有顏色sceme的顏色?
- 28. 如何在對話框中自定義android中CheckMark顏色的顏色。 :android
- 29. JavaScript在對話框加載後設置對話框標題欄顏色
- 30. Windows窗體:顏色對話框純色只
真的... WPF ...和沒有顏色選擇器...什麼... – serhio 2010-09-29 12:16:29
檢查[擴展WPF到olkit](http://wpftoolkit.codeplex.com/),它是WPF控件的集合。 – jwillmer 2012-04-18 08:27:10
[Extended WPF Toolkit](http://wpftoolkit.codeplex.com/)中有一個[ColorPicker Control](http://wpftoolkit.codeplex.com/wikipage?title=ColorPicker),它可以放在對話。 – SpeedCoder5 2014-03-06 20:13:14