Specify a design-time image source? - wpf

I am using SharpVectors to display SVG images in my WPF project.
One way to do this is to specify the SvgImage markup extension for the image source, such as:
<Image Source="{svgc:SvgImage Source=Images/test.svg}"/>
This works great at runtime.
However at design-time the image shows up blank. Is there any way to specify a design-time-only image? Ideally it would be the same image used at runtime, but even an alternative placeholder image would be better than nothing. That way at least the relative positions of objects would be clear, etc.
Note - although there are other ways of displaying an SVG image (namely, the SvgViewBox object) there are cases where you can only do it by specifying an image source. Namely, the LargeImageSource or SmallImageSource attributes of WPF Ribbon controls.

Providing the same answer here just for the records!
Please try using the newly added property; AppName, which is used to try and resolve the URI of the resource file at design-time.
See the samples for the SvgImage and newly added SvgImageConverter, especially the toolbar demo using the SVG icons.
https://github.com/ElinamLLC/SharpVectors/tree/master/TutorialSamples/ControlSamplesWpf
SvgImageConverter provides binding support, if you need it unlike the SvgImage.

Related

How to combined multiple PATH objects into a vector image that can be used as a style in WPF/XAML

I have a complex logo developed in Illustrator that I want to use as an image in WPF. I can seem to combine the Path's when importing into Blend to get a single Path without loosing all the fill colours.
What is the best way to convert the illustrator image into something that can be used in WPF/XAML.
Usually i just combine the Path's into a single Path and then reference the path using the following:
<Application.Resources>
<Path x:Key="pathKeyName" Data="..."/>
</Application.Resources>
<Path Style="{StaticResource pathKeyName}" />
However there seems to be no easy way to group multiple paths together into a single image that can be used as a resource in a similar manner.
Perhaps there is a way to convert these paths into a Geometry ?
EDIT
In the meantime I have used Blend to create a UserControl from the selected paths and then I place this control inside a Viewbox - not quite as simple but it seems to work.
No sweat, go grab yourself Mike Swanson's AI to XAML exporter plugin for Illustrator (dont pay attention to the versions mentioned on there, it works just fine with the newest version I use it all the time) and export your logo to XAML for WPF.
Then take that XAML and plop it in a ContentControl like I show how on a previous SO answer post.
Then you plop that Style Template into your resource dictionary or wherever and reference it as such with one line wherever/however you need it, voila. :)
Hope this helps, cheers.
OH PS, if there's a lot of shapes/colors etc, you'll never get it into just one Path, sorry amigo.

Setting default values for inherited properties on a custom control in WPF

I'm trying to subclass a charting tool that has on it an XAxis property, which is of type IAxis (so you can give it a NumericAxis, DateAxis, etc). When using the base chart normally, I can set the XAxis like so:
<chart:BaseChart>
<chart:BaseChart.XAxis>
<chart:NumericAxis VisibleRange="{Binding XAxisVisibleRange}" AxisAlignment="Bottom"/>
</chart:BaseChart.XAxis>
</chart:BaseChart>
However I'm not really sure what the best practice is for setting this default axis in a custom control, should it be in the XAML, in the C# code-behind, etc? I can get it working from the code-behind by setting the XAxis property/bindings in the constructor of my subclass. This works, I just don't know if it's the "correct" way to do it.
Being new to WPF, I don't know where/how to do it in XAML for the custom control, if that's even the right place for it to be. Should it go in the control template, the style, somewhere else? I tried doing a Setter in the style, that did not work (it compiled but I did not see any axes). From the control template, it doesn't recognize <chart:BaseChart.XAxis>. There are AxisAreas defined for top bottom right and left, and if I simply put the <chart:NumericAxis etc/> inside one of those then the axis does show up there, however that doesn't feel correct since it's not actually setting the XAxis property on the BaseChart, plus it's bypassing the AxisAlignment property on the NumericAxis object and explicitly placing the axis in whichever AxisArea I put the axis code in.
Like I said, I do have this working from the code-behind, I just want to make sure I'm doing it the best way I can. Any help would be much appreciated, thanks in advance!
Please take a look at the Control Authoring Overview page on MSDN for a complete answer to your question.
However, in short, for a CustomControl, you should create a folder in your application root directory named Themes. You should add a XAML file named Generic.xaml into this folder and you should put a ControlTemplate for your CustomControl in there.
For a UserControl, you can just add your Styles into the UserControl.Resources section of the page.

Winforms PropertyGrid per-GridItem row height

I'm using the Winforms PropertyGrid; the target of the SelectedObject includes a property of type Image. Everything is fine, except that with all items the same height, the image is too small to see properly. I'd like to have some control over the height of grid items such that the image can be displayed a bit larger. One other detail is that the SelectedObject of one PropertyGrid control may be assigned an object of any of a variety of different classes (which may or may not have image properties), so I'm hoping the height can be driven by data in the instance of the SelectedObject itself, rather than making it a static behavior of the control, although I'd settle for a custom attribute of the image property to make the item height at least class-specific if it can't be instance-specific.
How can I do this? Custom attribute? PropertyGrid event? Something else?
As Simon commented on your question, this is not possible to have a custom height for a GridItem.
You have 2 solutions to be able to show an image with a reasonable size:
You can code your own UITypeEditor. That way, the user would just click the down arrow and see a nicely sized image in the dropdown box.
Sorry for the plug but I think it directly answers your question: only 3rd party PropertyGrids may allow you to get variable size rows in the grid. Smart PropertyGrid.Net is one of them. You set a HeightMultiplier to the row so that it expands on let's say 4 rows. Then you code your own Look class that handles the drawing of the image the way you want in this space.

Forcing Arrange in a Custom WPF Canvas

I'm working on a project that involves creating a custom graphical editor. The graphical editor has multiple tabs, and I want to create a preview function that will show a popup with Bitmap previews of the content of each of the tabs. However, the problem I'm running into is that the content for each of the tabs must be arranged before a proper preview can be generated for it. This means going into each of the tabs to ensure that they're rendered and arranged. Then, and only then, do the previews get properly generated, otherwise the previews have a size of 0x0. Does anybody know how to force an arrange of a content control so that I can get a properly sized preview generated ?
Apparently you can just call the 'Arrange' method directly, but you have to be careful about the rectangle you pass in as a parameter, because it can adversely affect the display of your control if you just want to force the object to draw itself (ie can force it to draw out of the desired position).
Here is a link to MSDN where it discusses the "arrangeOverride" method of a control.
I'm not sure this is what you need, but this method seems to be what you're asking about.
Hope this helps!

Get XAML source of WPF Window

I would like to get the XAML source of a WPF Window (MainWindow). Clicking on a button on that window would return the XAML of the window and I would save it in another file.
Is this possible and how can it be achieved?
You can use the XamlWriter:
using (Stream stream = File.OpenWrite("D:\\Test.xaml"))
{
XamlWriter.Save(this, stream);
}
You can use XamlWriter for some basic Xaml Serialization.
In particular, look at this article on its limitations.
The earlier answers are both correct, but I think it should also be mentioned that you can also extract the original XAML used to create the window (if desired) using the API for Reflector's BAMLViewer extension.
BAMLViewer solves a different problem than XamlWriter: Using Reflector / BAMLViewer will return the original source XAML with all bindings, etc intact but will not include current property values. Using XamlWriter will include current property values but such things as resource references and markup extensions will be lost. Also, some things will not serialize using XamlWriter.
You must choose between these based on your application needs.

Resources