|
Hy! I'm trying to bind a given color to a rectangle in WPF... by now without success.
Here's my not working) approach:
XAML:
<Rectangle Fill="{Binding Path=color1}"
Margin="10,10,10,10"
Height="Auto" Width="Auto"
Grid.Column="0" Grid.Row="0"
/>
...
C# Code:
public Window2()
{
InitializeComponent();
bgColors.SetColors(Colors.Green, Colors.Green, Colors.Red, Colors.Red);
MainDockPanel.DataContext = bgColors;
}
and
internal class BGColors : INotifyPropertyChanged
{
private Color Color1;
public Color color1
{
get { return Color1; }
set { Color1 = value;
NotifyPropertyChanged("color1");
}
}
private Color Color2;
public Color color2
{
get { return Color2; }
set { Color2 = value;
NotifyPropertyChanged("color2");
}
}
private Color Color3;
public Color color3
{
get { return Color3; }
set
{
Color1 = value;
NotifyPropertyChanged("color3");
}
}
private Color Color4;
public Color color4
{
get { return Color4; }
set
{
Color2 = value;
NotifyPropertyChanged("color4");
}
}
public void SetColors(Color color1, Color color2, Color color3, Color color4)
{
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
this.color4 = color4;
}
private void NotifyPropertyChanged(string p)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(p));
}
}
public override string ToString()
{
return color1.ToString();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Binding the values as string to a label works perfectly, but no Colors can be assigned.
Help would be appreciated.
Thx in advance
|
|
|
|
|
When a data binding is not working, the first place to look is the Immediate Window. The message I got when trying your code is:
Cannot create default converter to perform 'one-way' conversions between types 'System.Windows.Media.Color' and 'System.Windows.Media.Brush'.
Either you will need to change the BGColors class to give brushes, or you will need to write a converter.
|
|
|
|
|
Also - make sure that the Color type you are binding is of the right kind. You can't bind a System.Drawing.Color to a WPF element - you need to use a System.Windows.Media.Color type. I got caught by this not so long ago and it had exactly the symptoms you describe - no problem reported however the color was ignored.
Melbourne. Australia.
modified 10-Jul-19 21:02pm.
|
|
|
|
|
|
Fill property of a Rectangle takes in a string. Try giving the name of color1 instead of the Color itself. It will work.
|
|
|
|
|
Hi there.
I want to convert Brushes.* to the hexadecimal numbers to using in HTML's files.
For example I'd like to convert Brushes.Black to the #000000 or Brushes.White to the #FFFFFF.
Can you help me ?
Freshman
|
|
|
|
|
Well - you can always get this using the Colors.A, Colors.R, Colors.G and Colors.B from the brush colour.
|
|
|
|
|
Thank you.
Can you write a sample code (Please)
I'm new
Freshman
|
|
|
|
|
Thinking about it, the simplest way is to do this:
SolidColorBrush brush = Brushes.Aqua ;
string color = string.Format("#{0}",brush.ToString().Substring(3)); The brush includes an alpha component at the start, so you need to remove it from the text. This means you need to do a substring on the ToString() which returns the full ARGB version of the colour.
|
|
|
|
|
Nice
You're expert in WPF.
Thank you very much Pete
Freshman
|
|
|
|
|
I have the Implementaion for Displaying treeListView but, don't know how to do DragDrop in it??
|
|
|
|
|
Josh has an excellent article here[^] that can be easily adapted to treeview drag/drop.
|
|
|
|
|
hi,
how we can set F1 key for image button to open chm file in wpf
|
|
|
|
|
I have got it.
Add Window.CommandBindings in xaml file.
Add tag CommandBinding Command='ApplicationCommands.Help' Executed='OnClickContents' as the child of </Window.CommandBindings>
We can write code under event handler for event Executed.
|
|
|
|
|
Hello friends.
I want to get richTextbox's text in wpf.
How can I do it ?
Something like this : richTextBoxText.Text
Freshman
modified on Wednesday, July 2, 2008 1:26 AM
|
|
|
|
|
I have created flowdocument name="flowData" as the child of rich texrt box and paragraph as the child of Flow document in the xaml file.
In the cs file I have created TextRange object and passed the flowdocument.ContentStart and flowdocument.ContentEnd
eg: TextRange documentRange = new TextRange( flowData.ContentStart, flowData.ContentEnd );
then documentRange.Text returns the data entered in rich text box.
|
|
|
|
|
Did you mean :
RichTextBox rtb = richTextBoxText as RichTextBox;
TextRange tr = new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd);
Freshman
|
|
|
|
|
Hi there.
I want to convert "Black" string to Brush in WPF.
How can we do it?
Freshman
|
|
|
|
|
One way is to use the BrushConverter:
BrushConverter conv = new BrushConverter();
SolidColorBrush brush = conv.ConvertFromString("Black") as SolidColorBrush;
|
|
|
|
|
Wow, nice reply.
Thank you.
Freshman
|
|
|
|
|
|
Don't cross post.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
---modified. Original problem with this is solved, but I got a new problem with the same xaml ---
Ok I just started in WPF so it might be very obvious.
I made a UserControl which contains also a ToggleButton, when that togglebutton is pressed I want to alter the background of the border control.
<UserControl x:Class="StoreDBViewer.MyTableControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<Border x:Name="MyCtrlBorder" BorderThickness="1" BorderBrush="Gray" Background="Green">
<!-- Here a bunch of controls and stuff is added -->
<ToggleButton x:Name="btnToggle" Margin="5,5" Click="OnToggleColView">
Button text
</ToggleButton>
</Border>
</Grid>
<UserControl.Triggers>
<Trigger SourceName="btnToggle" Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="MyCtrlBorder" Property="Border.Background" Value="Blue" />
</Trigger>
</UserControl.Triggers>
</UserControl>
This compiles correctly, but as soon as I run the app an exception is thrown( the exception is not thrown if I comment out the Trigger part).
Does anyone know what might be wrong here?
Regards,
Davy
modified on Monday, June 30, 2008 8:55 AM
|
|
|
|
|
What is the error message of the exception (or the message of the InnerException, or the InnerException of the InnerException, or however many levels it takes to find something useful)? In cases like this, the error messages usually have a good pointer for where to start looking.
|
|
|
|
|
Thanks for the reply.
The message of the exception itself was pretty useless, but following the innerexceptions 3 levels up I came to this message:
Triggers collection members must be of type EventTrigger.
So doing some research on that I found that this kind of trigger only works in ControlTemplate,DataTemplates and Style... So I guess I have to reorder some stuff in the xaml file to fit the triggers somewhere in a style or so.
Regards,
Davy
|
|
|
|