Color Picker Combo Box - combobox

I want to customize my combobox each item, same as font, background, etc.
Is there any simple way to set custom background color of each Combobox item?

This is simple example to do that. In this example my combobox has some item same as color name (Red, Blue, etc) and change the background of each item from this. Just flow the steps:
1) Set the DrawMode to OwnerDrawVariable:
If this property set to Normal this control never raise DrawItem event
ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
2) Add an DrawItem event:
ComboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
3) Entry your own code to customize each item:
private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds; //Rectangle of item
if (e.Index >= 0)
{
//Get item color name
string itemName = ((ComboBox)sender).Items[e.Index].ToString();
//Get instance a font to draw item name with this style
Font itemFont = new Font("Arial", 9, FontStyle.Regular);
//Get instance color from item name
Color itemColor = Color.FromName(itemName);
//Get instance brush with Solid style to draw background
Brush brush = new SolidBrush(itemColor);
//Draw the item name
g.DrawString(itemName, itemFont, Brushes.Black, rect.X, rect.Top);
//Draw the background with my brush style and rectangle of item
g.FillRectangle(brush, rect.X, rect.Y, rect.Width, rect.Height);
}
}
4) Colors need to be added as well:
ComboBox1.Items.Add("Black");
ComboBox1.Items.Add("Blue");
ComboBox1.Items.Add("Lime");
ComboBox1.Items.Add("Cyan");
ComboBox1.Items.Add("Red");
ComboBox1.Items.Add("Fuchsia");
ComboBox1.Items.Add("Yellow");
ComboBox1.Items.Add("White");
ComboBox1.Items.Add("Navy");
ComboBox1.Items.Add("Green");
ComboBox1.Items.Add("Teal");
ComboBox1.Items.Add("Maroon");
ComboBox1.Items.Add("Purple");
ComboBox1.Items.Add("Olive");
ComboBox1.Items.Add("Gray");
You can change the rectangle size and position to draw your own item as you want.
I hope this post is useful.

Related

Wpf text rotation

I want to add some text on a canvas and decided to use textBlock for that (to set Font etc.)
But I cannot figure out how to rotate it.
I use the following function to add myText on myCanvas:
void text(double x_pos, double y_pos, string myText, double angle, Point rot_cen, Color color1)
{
TextBlock textBlock = new TextBlock()
{
Text = myText,
FontFamily = new FontFamily("Verdana"),
FontSize = 16,
TextAlignment = TextAlignment.Center
};
textBlock.Foreground = new SolidColorBrush(color1);
Canvas.SetLeft(textBlock, x_pos);
Canvas.SetTop(textBlock, y_pos);
textBlock.RenderTransform = new RotateTransform(angle, rot_cen.X, rot_cen.Y);
myCanvas.Children.Add(textBlock);
}
From what I've read rot_cen is a point from (0,0) - which is top left corner to (1,1) - which is bottom right corner. But when i set it to be (0.5,0.5) it still rotates around top left corner. Do I need to update it somehow?
The CenterX and CenterY properties of a RotateTransform use absolute coordinates.
You may want to set RenderTransformOrigin, which uses relative coordinates:
textBlock.RenderTransformOrigin = rot_cen;
textBlock.RenderTransform = new RotateTransform(angle);

Convert IEnumerable<Color> into a brush

I have an IEnumerable of Color which I want to use as the basis for a brush.
At the moment, I convert the IEnumerable into a Bitmap, into a bitmapsource, into an imagebrush, but this is a bit slow, is there any brush class which can do what I want in a faster way?
edit, What I want to do: Use the brush in a pen to draw a line in a drawing visual, where the IEnumerable of Color is used as line color. If I have a collection of { Colours.Green, Colours.Red}, I want the resulting line to be half green, half red.
Here's a method that would convert your IEnumerable to a LinearGradientBrush.
There are 2 gradient stops for each color in order to create a hard transition between colours rather than a gradient.
LinearGradientBrush CreateBrush(IEnumerable<Color> colors) {
var colorArray = colors.ToArray();
var step = 1.0 / colorArray.Length;
var gradientStops = new GradientStopCollection();
for (int i = 0; i < colorArray.Length; i++) {
var color = colorArray[i];
gradientStops.Add(new GradientStop(color, i * step));
gradientStops.Add(new GradientStop(color, (i + 1) * step));
}
return new LinearGradientBrush(gradientStops);
}

Set element opacity different than window

I'd like to know if it's possible to:
I have window (Window1) with listview. Double click on element (Element1) of this listview open little popup window (Window2).
I'd like to set Element1 and Window2 opacity to 1, but Window1 to 0.2
Window2 is open as topmost with ShowDialog().HasValue, like
this.Opacity = 0.2;
selected.opacity = 1;
Window2.opacity = 1;
if(Window2.ShowDialog().HasValue())
this.Opacity = 1;
#EDIT:
main window, called "Window1":
private void Border_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
if (popup != null)
popup.Close();
popup = new PopupWindow(ListView.SelectedItem as SelectedItem, sender as Border, this);
popup.Topmost = true;
((Border)sender).Opacity = 1;
this.Opacity = 0.2;
popup.Opacity = 1;
if (popup.ShowDialog().HasValue)
{
this.Opacity = 1;
}
}
}
Unfortunately, what you are trying to achieve cannot be directly accomplished with WPF because Opacity values are kind of inherited by child controls. From the UIElement.Opacity Property page on MSDN:
Opacity is applied from parent elements on down the element tree to child elements, but the visible effects of the nested opacity settings aren't indicated in the property value of individual child elements. For instance, if a list has a 50% (0.5) opacity and one of its list items has its own opacity set to 20% (0.2), the net visible opacity for that list item will be rendered as if it were 10% (0.1), but the property value of the list item Opacity property would still be 0.2 when queried.
However, it is possible to fake your desired look by making certain elements within the Window semi opaque, while still having Opacity="1.0" for child elements. So, try removing the Opacity setting from the Window and instead set the Background to a see through colour like this:
window.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
Or even simpler:
window.Background = Brushes.Transparent;
Using a combination of transparent colours and low Opacity values on certain UI elements should get you what you want eventually.

How to create ColorAnimation without fade effect

I want to change the background color of a rectangle to green for 1 second than change it back to black. I want to simulate a light being on or off - I dont want to fade the color in. The code below does what I want except it fades from black to green and vice versa. I definately dont want sleep on the UI thread..........
ColorAnimation animation = new ColorAnimation { From = Colors.Black, To = Colors.LightGreen, Duration = new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior= new RepeatBehavior(1), AutoReverse=true };
SolidColorBrush activityLight = new System.Windows.Media.SolidColorBrush(Colors.Black);
ActivityIndicator.Fill = activityLight;
this.RegisterName("activityLight", activityLight);
ActivityStoryboard = new Storyboard();
ActivityStoryboard.Children.Add(animation);
Storyboard.SetTargetName(animation, "activityLight");
Storyboard.SetTargetProperty(animation, new PropertyPath(SolidColorBrush.ColorProperty));
You could use a ColorAnimationUsingKeyFrames:
var colorAnimation = new ColorAnimationUsingKeyFrames();
colorAnimation.KeyFrames.Add(
new DiscreteColorKeyFrame(Colors.Green, TimeSpan.FromSeconds(0d)));
colorAnimation.KeyFrames.Add(
new DiscreteColorKeyFrame(Colors.Black, TimeSpan.FromSeconds(1d)));
ActivityIndicator.Fill.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);

How can I display multiple colored underlined Text in a Textbox

this code underlines all Text in the Textbox, can I underline only specific Text?
Brush brush = Brushes.Blue;
Pen pen = new Pen(brush,2);
TextBox tb1 = new TextBox();
tb1.AcceptsReturn = true;
tb1.Text = "This is a very long Text not?";
TextDecoration textDec = new TextDecoration(TextDecorationLocation.Underline,pen,1,TextDecorationUnit.Pixel,TextDecorationUnit.FontRecommended);
tb1.TextDecorations.Add(textDec);
tb1.Width = 400;
tb1.Height = 30;
this.AddChild(tb1);
The TextBox doesn't provide the ability to alter characteristics for individual characters. It's an all or none control.
The RichTextBox is the control you need.

Resources