I have a work area (grid) where I add and remove objects at runtime, mostly canvases or stackpanels containing different paths.
When I have dragged 'n' dropped all objects I want to my 'drawing', I want to save the data to the database.
I use XamlWriter.Save() to get the string data and it works "fine", but all Path Data is empty:
XAML for Path Data is not available due to Silverlight runtime limitations
<Path StrokeThickness="0.98">
<Path.Data>
<PathGeometry>
<!-- XAML for Path Data is not available due to Silverlight runtime limitations -->
</PathGeometry>
</Path.Data>
<Path.Stroke>
<SolidColorBrush Color="#FF000000">
<SolidColorBrush.Transform>
<MatrixTransform>
<MatrixTransform.Matrix>
<Matrix />
</MatrixTransform.Matrix>
</MatrixTransform>
</SolidColorBrush.Transform>
<SolidColorBrush.RelativeTransform>
<MatrixTransform>
<MatrixTransform.Matrix>
<Matrix />
</MatrixTransform.Matrix>
</MatrixTransform>
</SolidColorBrush.RelativeTransform>
</SolidColorBrush>
</Path.Stroke>
</Path>
Is there any way around this?
I really need to get the path data to be able to save and then later open and continue working on my 'drawing'.
Help and/or other ideas are very welcome..!
Thanks in advance!
//Anna
If RobSiklos' comment doesn't help, you might have to either serialize that data in another way, or manually post process the XML and add the path data yourself.
Related
If I have a rather meandering Path in my WPF app, is there a way I can make it appear as two differently-colored Paths of identical widths side-by-side? I'd rather not try to hand-code the whole thing again with slightly different values. I thought of using a Brush, but the list of Brushes doesn't appear to have one such.
Edit: I want a Path divided sharply by color, even if it curves, like this:
Made a little search, and found that also :
Two-color Path object
Timwi answer :
<StackPanel Orientation="Horizontal">
<StackPanel.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleX="15" ScaleY="15" />
</StackPanel.LayoutTransform>
<Grid Margin="-5,0,0,0">
<Path Fill="Blue" Stroke="Transparent">
<Path.Data>
<PathGeometry>M10,10 C20,10 10,20 20,20 L20,19 C11,19 21,9 10,9</PathGeometry>
<!-- |← original path →| |← generated part →| -->
</Path.Data>
</Path>
<Path Fill="Red" Stroke="Transparent">
<Path.Data>
<PathGeometry>M10,10 C20,10 10,20 20,20 L20,21 C9,21 19,11 10,11</PathGeometry>
<!-- |← original path →| |← generated part →| -->
</Path.Data>
</Path>
</Grid>
</StackPanel>
So "playing" with margin may be much easier that the other options I told you about for what you need.
DropShadowEffect solved my issue.
Issue
I am attempting to write a style/theme for an application
This is based of a mergedResourceDisctionary
My intention is to inherit dynamic resources from an existing Base Dynamic resource of the same type, in this case
STAC_RGrad_Status_Base
In the example below I have 2 other RadialGradientBrushes all sharing exactly the same properties except the GradientStops
I have already gone some way to inheriting, (See the STAC_RGrad_Status_Green) however is there a simpler way than this where I can replace all 4 Binding statements into a "One Liner" statement which is interpreted as
Properties="{Binding Propterties , Source={DynamicResource STAC_RGrad_Status_Base}} except GradientStops"
Which will allow future proofing should I need to add an additional property to that tree of resources defined in
So instead of creating a binding for each property as I have done in the example
It is replaced by a "one liner"
Existing Code
<RadialGradientBrush x:Key="STAC_RGrad_Status_Base"
GradientOrigin="0.5,0.5"
Center="0.5,0.5"
RadiusX="0.5"
RadiusY="0.5"
GradientStops="{DynamicResource STAC_Grad_Status_Disabled}"
/>
<RadialGradientBrush x:Key="STAC_RGrad_Status_Red"
GradientOrigin="0.5,0.5"
Center="0.5,0.5"
RadiusX="0.5"
RadiusY="0.5"
GradientStops="{DynamicResource STAC_Grad_Status_Red}"
/>
<RadialGradientBrush x:Key="STAC_RGrad_Status_Green"
GradientOrigin="{Binding GradientOrigin, Source={DynamicResource STAC_RGrad_Status_Base}}"
Center="{Binding Center, Source={DynamicResource STAC_RGrad_Status_Base}}"
RadiusX="{Binding RadiusX, Source={DynamicResource STAC_RGrad_Status_Base}}"
RadiusY="{Binding RadiusY, Source={DynamicResource STAC_RGrad_Status_Base}}"
GradientStops="{DynamicResource STAC_Grad_Status_Green}"
/>
I want to include an image in my project from behind-code, in order to permit me to set the image as fill of my rectangle!
I'm inserting an image in this way:
<Rectangle Grid.Column="2" Grid.Row="2" Margin="4,4,4,4" Style="{DynamicResource rectangle_style}" >
<Rectangle.Fill>
<ImageBrush ImageSource="image.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
For create this background image I must include the image file in a project with right click on file and set "include in a project".
Can you help me to include the image from behind-code in run time, because I will have necessity to set image from most file in a folder!
One way is to do "Include in Project" or "Add existing item" and under Properties, set "Copy Local" to Always.
Note in this case, you need to get the application folder path
<ImageBrush ImageSource="c:\[YourPath]\[YourProject]\someImg.png" Stretch="UniformToFill" />
Or bind to a method that provides the current application path for you. (Do a search for this as there are many such questions answered on SO).
This means when you deploy the app, you have to deploy the image with it (it'll end up in the bin folder with whatever path structure you give it).
Another way is to do the same, and set Build Action to Resource - this will make your application compile with the image inside the DLL. Downside is the DLL will be larger, but you won't have to remember to copy the file when you deploy.
Access the resource by name (no front slash)
<ImageBrush ImageSource="someImg.png" Stretch="UniformToFill" />
Hope it helps!
I'm using the M-V-VM pattern in WPF and I have a background brush I'm going to be using rather often and I'd like to move it out in to a shared ResourceDictionary.
The only problem is the brush uses a color which it gets via Databinding to its hosted context.
Is there anyway I can move the brush out in to a ResourceDictionary and still have it find the value it needs?
The Brush:
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5"
CenterY="0.5"
ScaleX="2.3"
ScaleY="2.3" />
<TranslateTransform X="-0.3"
Y="-0.3" />
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Color="{Binding Path=BackdropColor}"
Offset="1.2" />
<GradientStop Color="#FFFFFFFF"
Offset="-0.1" />
</RadialGradientBrush>
After re-factoring it out to a ResourceDictionary and adding a key, I called it as such:
<StackPanel Grid.Row="0"
Margin="0,0,0,0"
Orientation="Horizontal"
Background="{DynamicResource BackdropRadGradBrush}">
But this resulted in this output in the debugger:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=BackdropColor; DataItem=null; target element is 'GradientStop' (HashCode=16001149); target property is 'Color' (type 'Color')
I don't think that you can keep this Brush in your resource dictionary and use binding to pull in the color. Since the brush is only created once (which is why you want it in the resource dictionary in the first place), at the time of creation WPF doesn't know where it will be used, so it can't pull in the value for the color.
If the color were kept in Setings, for example, that would probably work - but I'm guessing that won't help you, because you probably want the color to change on each control that it is used on (otherwise, you could just hard code the color or it would already be in settings).
Maybe you could create a RadialGradientBrush subclass, and expose the first GradientStop color as a DependencyProperty? You could then create an instance of this subclass wherever you need it, and use binding to pull in the correct color there.
This is a little late, but take a look at using a StaticResource or a DynamicResource instead of a Binding - it will allow you to access another Resource. Not quite Binding, but it's better than nothing.
If I had a Canvas with n number of Visual objects of any shape or size, how would I highlight/outline a Visual object programmatically?
Is there something built into WPF to help me?
Having read the comments in the other answer -- in which you state that you wish the outline to conform to the shape of the visual -- I can only suggest you take a look at the BitmapEffects. There is one for an Outer Glow effect.
Duplicating the visuals themselves would probably be less efficient, and would probably produce a great number of complications due to other aspects of those items, such as any bindings.
Edit: In .NET 4.0, BitmapEffects property and the BitmapEffect class are obsolete. You'll get an exception thrown. Instead, you now have the System.Windows.Media.Effects.Effect class and its derived classes, along with properties such as Visual.VisualEffect.
Joel has a great suggestion about using BitmapEffects.
However, if you can use .NET 3.5 SP1, I would steer you toward GPU rendered effects. So, instead of using UIElement.BitmapEffect, you would use UIElement.Effect. .NET 3.5 SP1 has two GPU effects built in: BlurEffect and DropShadowEffect. You can also create your own GPU effects ... and this is where you would use ShaderEffect.
Check out Greg Schechter's blog post series for more info.
Here is a piece of code for a mouse over effect that I just implemented a few days ago (it actually shows up sort of like an outer glow bitmap effect due to the ShadowDepth being 0, but not exactly):
<Path
x:Name="mouseOverEffect"
Width="80"
Height="43.916"
Stretch="None"
Fill="#FFFFFFFF"
Opacity="0"
>
<Path.Data>
<PathGeometry FillRule="Nonzero">
<PathFigure IsClosed="True" StartPoint="39.9592899612151,25.9913931634531">
<LineSegment Point="80.0000001464848,43.9159987905149"/>
<LineSegment Point="39.9513899394755,4.97379893856246E-14"/>
<LineSegment Point="1.77635636294422E-15,43.9159987905149"/>
<LineSegment Point="39.9592899612151,25.9913931634531"/>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Effect>
<DropShadowEffect
Color="#FFFFFFFF"
BlurRadius="10"
ShadowDepth="0"
/>
</Path.Effect>
</Path>
If you wrap those Visual objects in a Border ... and make the Opacity 0 by default ... then you could programmatically turn the Opacity to 1 whenever you need to.
Does that help?