Property path can't find reference - wpf

What's the syntax for the property path to find the color property of the second gradientStop?
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="#FFD0D0D0" Offset="0.992" />
</RadialGradientBrush>
</Rectangle.Fill>
I tried New PropertyPath("Fill.RadialGradientBrush.GradientStops[1].Color") but it could find the color property.

In this case, the Fill property is the RadialGradientBrush. The brush is not a member of Fill.
Try PropertyPath("Fill.GradientStops[1].Color")

I guess it should be :
(Fill as RadialGradientBrush).GradientStops[1].Color
Because RadialGradientBrush is not a property, but a class

Related

Cannot find governing FrameworkElement or FrameworkContentElement for target element. GradientStop

I have a list box contains Colors and Each Color have 7 Heads it is to choose Print Head for Color. so my Binding variable is when print head is selected , Color box should have this Styles.
<Setter TargetName="colorSelectionRectangle" Property="Fill">
<Setter.Value>
<LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,0" EndPoint="25,25">
<LinearGradientBrush.RelativeTransform>
<ScaleTransform ScaleX="0.01" ScaleY="0.01" />
</LinearGradientBrush.RelativeTransform>
<GradientStop Offset="0" Color="White" />
<GradientStop Offset="0.5" Color="White" />
<GradientStop Offset="0.5" Color="{Binding [0].Item.PrintColor.Argb}" />
<GradientStop Offset="1" Color="{Binding [0].Item.PrintColor.Argb}" />
</LinearGradientBrush>
</Setter.Value>
and My error is :
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=[0].Item.PrintColor.Argb; DataItem=null; target element is 'GradientStop' (HashCode=52327179); target property is 'Color' (type 'Color')
I believe that you have this problem because GradientStop is not a FrameworkElement... from MSDN:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.GradientStop
If you notice the extended System.Windows.Freezable class above, then you will see that this class is also 'freezable'... this means that it cannot be modified. See the Freezable Objects Overview page at MSDN for more information.

WPF change dynamicresource in codebehind

The standard RadioButton does not support setting the color of the ellipse. So, I took a radiobutton template from this location as a basis for a custom RadioButton:
RadioButton Styles and Templates
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource ControlLightColor}" />
<GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
ControlLightColor and ControlMediumColor are defined as:
<Color x:Key="ControlLightColor">#ffff9a</Color>
<Color x:Key="ControlMediumColor">#ffff9a</Color>
Which gives us a yellow-ish ellipse.
How can I alter this color in the codebehind?
Regards,
Michel
Create a style by following this:
Creating a Style in code behind
then assign it to your element.Style
You can also access resources by
Resources["mykey"]
Solution:
<Ellipse x:Name="Border" StrokeThickness="1" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.RadioButtonColor}">
Public ReadOnly Property RadioButtonColor() As SolidColorBrush
Get
Dim solidColorBrush As SolidColorBrush
If MyBusinessLogic Then
solidColorBrush = _radioButtonNotRequiredBrush
Else
solidColorBrush = _radioButtonRequiredBrush
End If
Return solidColorBrush
End Get
End Property
Thumbs up for JRB for thinking along.

WPF control that fades out any overflowing text to transparent, rather than clipping it or wrapping?

How can I create a WPF control (similar to a TextBlock) so that any overflowing text is faded out to transparent rather than simply clipping or wrapping?
I need to keep my control fixed-width, so expanding the width of the control to fit the text is not an option. I also do not want to make the text font smaller.
Not quite sure exactly what you are trying to achieve, but you could do something like this:
<TextBlock Text="Some long text here that should fade out">
<TextBlock.Foreground>
<LinearGradientBrush>
<GradientStop Offset="0" Color="Black"/>
<GradientStop Offset="0.7" Color="Black"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
But your control still needs to be wide enough to accommodate all the text for it to display.
<TextBlock Text="fgdfgfdgfddgfdgdfgfdgfdgd" Width="129" TextWrapping="NoWrap">
<TextBlock.Foreground>
<LinearGradientBrush EndPoint="0.661,0.399" StartPoint="0.008,0.496">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#7F000000" Offset="0.803"/>
<GradientStop Color="#4C0A0909" Offset="0.95"/>
<GradientStop Color="#BF000000" Offset="0.729"/>
<GradientStop Color="#F8000000" Offset="0.699"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
The trick on the gradient is that even though all colors are all based off black, The fade is achieved through opacity of each gradient by playing with A part of RGBA, in pseudo-code:
GradientStop Color="Black" A=100%
GradientStop Color="Black" A=97% Offset="0.803"
GradientStop Color="Black" A=75% Offset="0.95"
GradientStop Color="Black" A=80% Offset="0.729"
GradientStop Color="Black" A=30% Offset="0.699"
Thanks guys, but I found the answer I needed on MSDN.

Partial-filling of a rectangle on Silverlight

I am new in Silverlight. I need to fill a rectangle partially with a SolidColorBrush (like a Bar, I need the border). With GradientBrush, the change of color is not sharp.
I can add two rectangles for this, but there has to be a better way.
Any suggestion would be appreciated.
I am using Silverlight 4.0.
Thanks for reading.
-Rakib
What about using gradient brush with gradient stops very close to each other, something like this:
<LinearGradientBrush x:Key="_backgroundBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Yellow" Offset="0.65" />
<GradientStop Color="Blue" Offset="0.66" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>

Using a StaticResource SolidColorBrush to define the Gradient Stop Colors

I am creating some wpf resource dictionaries with all the styles for an application! I have a few LinearGradientBrushes, where the color is set directly in the LinearGradientBrush reference as GradientStops. However, I want to have a predefined set of colors that I can use a a reference for each GradientStop, so that changing the color scheme for the application is a matter of changing the values of the SolidColorBrushes:
<SolidColorBrush Color="#5A5A5A" x:Key="colorbrushMedium" />
<SolidColorBrush Color="#222222" x:Key="colorbrushDark" />
<LinearGradientBrush>
<GradientStop Color="{StaticResource colorbrushMedium}"/>
<GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>
With the code example above, I am getting the following error:
Cannot convert the value in attribute 'Color' to object of type 'System.Windows.Media.Color'. '#5A5A5A' is not a valid value for property 'Color'.
The line it refers to is the line where <GradientStop Color="{StaticResource colorbrushMedium}"/> is defined.
Any ideas?
Ok, I found the problem:
Using Color and not SolidColorBrush..
<Color x:Key="colorbrushMedium">#FF5A5A5A</Color>
<Color x:Key="colorbrushDark">#FF222222</Color>
<LinearGradientBrush>
<GradientStop Color="{StaticResource colorbrushMedium}"/>
<GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>
This seems to solve my problem!
Use Binding to reference the color both in SolidColorBrush and in LinearGradientBrush:
<SolidColorBrush x:Key="stop1" Color="#FF5A5A5A"/>
<SolidColorBrush x:Key="stop2" Color="#FF222222"/>
<LinearGradientBrush x:Key="gradient">
<GradientStop Color="{Binding Source={StaticResource stop1},Path=Color}" Offset="0"/>
<GradientStop Color="{Binding Source={StaticResource stop2},Path=Color}" Offset="1"/>
</LinearGradientBrush>

Resources