Apr 22 2008 3:25PM GMT
Posted by: Mark Shurmer
WPF,
XAML,
printing
When you get to printing in WPF, you will find it so much easier than printing in previous Microsoft UI design (ie Winforms and MFC). See a previous post of mine to see how easy.
One thing that does occur though, is that when you print your element/canvas/grid/whatever, it will usually go on the printer very small. Why is that?
Actually, I think it is a good thing 
What it means is that it’s printing exactly the same on the printer as on the screen, something we have been begging for, for ages [well since we’ve had GUI’s anyway].
So how can you make it bigger, well it’s pretty easy when you know how:
You simply make the element bigger by using a Transform before printing, like
grid.LayoutTransform = new ScaleTransform(6,6);
printDlg.PrintVisual(grid, “My vision”);
Apr 11 2008 2:48PM GMT
Posted by: Mark Shurmer
WPF,
XAML,
printing
To do a simple print in WPF, you can do the following:
PrintDialog prtDlg = new PrintDialog();
if(prtDlg.ShowDialog() == true)
{
prtDlg.PrintVisual(element, “A simple drawing”);
}
This will throw up the XP or Vista print dialog, and then print the element specified, no problems at all.
Actually there are a couple of problems:
- The element is always lined up in the top left of the printed page
- If you haven’t specified any margin, the element might get cut off
- There’s no pagination
- It uses the same device independent system for printing as showing on the screen ,1:96, so an element 96 pixels wide will appear 1 inch wide on the printed paper
Apr 11 2008 2:37PM GMT
Posted by: Mark Shurmer
WPF,
XAML,
printing
How does printing work with WPF?
Well, actually, if anyone out there has ever worked with printing in MFC, then it’s so advanced as to make you dizzy.
Even if you ever tried printing with Windows Forms, then printing in WPF is a bit of a revelation.
So how does it work?
Most of the printing stuff is in the System.Printing namespace, and a good starting place is the PrintDialog class.
By instantiating the PrintDialog class, you can display the standard xp or vista print dialog to allow the user to choose where to print something and how.
What else does it give you?
You can call two different methods to do printing:
PrintVisual - allows you print any class that derives from System.Windows.Media.Visual - which is a lot 
PrintDocument - allows you to print any DocumentPaginator, e.g. FlowDocument or XpsDocument.
And it just works 
Apr 3 2008 11:02PM GMT
Posted by: Mark Shurmer
WPF,
XAML,
VS 2008,
Microsoft Windows
I have recently rediscovered Document Outline in VS2008.
It was something that I relegated, in my head at least, to the world of web
However, I thought about a week ago - wouldn’t it be very handy if it showed me my complex xaml in a tree.
Lo and behold it did, andI could click on each node and go straight to the relevant DataTemplate
It was a nice and clean way to navigate the page, and dare I say it, easier than using Blend.
Obviously Blend has a lot of great features not in VS2008, but is a bit designer like for me sometimes
Mar 31 2008 11:03PM GMT
Posted by: Mark Shurmer
WPF,
Databinding,
XAML,
Validation
Whether to use IDataErrorInfo or ValidationRule - that is the question, as I posed in a previous post.
So , you may be wondering what do I do in my WPF projects?
Or you may not
Well, I tend to use both!
I use IDataErrorInfo to provide validation in the business object itself, then derive extra classes from ValidationRule when I want validation across different business objects.
I have tried making the Validation classes always separate from the pure data business classes, but I found it to be counter productive and also counter intuitive.
Mar 31 2008 11:01PM GMT
Posted by: Mark Shurmer
Validation,
Databinding,
WPF,
XAML,
Microsoft Windows,
Windows Computing
As I mentioned in a previous post, you can specify validation by creating a Validation class (or classes).
You do this by deriving a new class from the ValidationRule class and overriding the Validate method.
Why would you do this, instead of implementing IDataErrorInfo?
Well, if you wanted to do validation across a number of objects, then this is the way to go.
It also provides a level of separation from your business objects, leaving them to deal with business data and the validation objects to deal with validation.
A downside is that it is not backwardly compatible with WinForms
How do you tell your xaml to use it? By specifying it in the ValidationRules element of your binding - just like IDataErrorInfo.
Mar 26 2008 8:49AM GMT
Posted by: Mark Shurmer
WPF,
Databinding,
XAML
As per a previous post, how do you do data validation in WPF using IDataErrorInfo?
Well what does IDataErrorInfo give you? It defines two properties:
public string Error
{
get;
}
public string this[string columnName]
{
get;
}
When you implement these, you provide error validation for your WPF application.
How does that happen?
Well, it doesn’t automatically of course [no surprises there].
When you specify your binding in xaml, you need to specify either the ValidationRules element or the ValidatesOnDataErrors parameter:
<Binding Source=”{StaticResource tradeList}” Path=”Instrument” UpdateSourceTrigger=”PropertyChanged”>
<Binding.ValidationRules>
<DataErrorValidationRule>
</Binding.ValidationRules>
</Binding>
or
<Binding Source=”{StaticResource tradeList}” Path=”Instrument” ValidatesOnDataErrors=”true” />
Mar 14 2008 4:50PM GMT
Posted by: Mark Shurmer
WPF,
XAML,
Validation
There seems to be four ways to go with validation with WPF:
- Use IDataErrorInfo
- Use custom classes with ValidationRule
- Combine 1 and 2
- Roll your own
What do I mean by each of these, and why?
- Use IDataErrorInfo. You canimplement this interface in your business objects and you get the opportunity to specify validation for each property or for all properties. You do this in (usually) a general purpose method that does validation for all of your properties in your business object. This is good if you need validation for a business object, but not across objects. You can tell wpf to use it (in v3.5) by specifying ValidatesOnDataErrors=”True” in your binding specification.
- Inherit a class from ValidationRule, and provide an override for the Validate method. You could do this for either a particular business object, or across a number of objects. You can use it in xaml by specifying it in the binding’s ValidationRules collection.
- Do 1 for business object validation, and 2 for any validation that needs to be done across business objects
- Do this when you need to extend the validation, e.g. like in the CSLA.Net framework
Mar 10 2008 9:39PM GMT
Posted by: Mark Shurmer
Databinding,
WPF,
XAML
How do you use the same data template across different types of control?
Well you can
Normally when you define the data template, you do something like the following:
<DataTemplate DataType=”{x:Type ListViewItem}>
<TextBlock Text=”{Binding Path=ISIN}” />
</DataTemplate>
However, it is a bit of a pain to then copy it for different types, but there is a way:
<DataTemplate DataType=”{x:Type local:Instrument}>
<TextBlock Text=”{Binding Path=ISIN}” />
</DataTemplate>
(where Instrument is a class in your assembly)
This template can now be used with any control type.