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 14 2008 3:47PM GMT
Posted by: Mark Shurmer
WPF
I went to DevWeek08 this week, and saw some excellent presentations, and some not so….
I particularly liked the talks by Christian Weyer and Richard Blewett on .Net 3.5
However, there wasn’t a huge amount of WPF stuff. I saw a couple of talks by David Wheeler, just to see how it was panning out in the real world. They were very popular, so I wonder if the organisers might have put on more WPF talks. One of the talks was on multi-threading in WPF, and Dave was a very entertaining talker although the content wasn’t that deep due to time constraints.
Silverlight was overshadowing WPF, as I suspect it will in the general world of IT 
The talks on Silverlight by Jeff Prosise were also excellent, and his enthusiasm for the subject matter was clear.
It has made me realise two things that I didn’t know:
1. A beta of Silverlight was released last week (for MIX08 in USA I believe), and actually has some decent stuff in it. And it works 
2. It really is cool :-), so I think I might do some posts on the experienc
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.
Mar 9 2008 6:53AM GMT
Posted by: Mark Shurmer
XAML,
C,
Databinding,
WPF
Here’s a nifty little trick that I recently discovered.
When you need to calculate the result of two properties and display it, you can do it via a Converter class.
You may want to do it, like me, if you couldn’t update the business objects, or didn’t want to.
How do you do it?
Your converter class needs to implement IMultiValueConverter instead of IValueConverter.
E.g.:

Then you specify a multi binding in your xaml, like:

Mar 6 2008 2:45PM GMT
Posted by: Mark Shurmer
WPF,
Databinding,
XAML
In a previous post, I described how you can get a binding to update in real time - i.e. as you type.
Sometimes however, you want to control the updating more closely, i.e. in code. For example, you may want to do the update only when a timer has ticked.
There is a little used argument for UpdateSourceTrigger (well in 18 months of using WPF I’ve never seen it used :-)) , namely Explicit that allows you to do that.
Given some xaml like:
<ComboBox Name=”cbUsers” SelectedValuePath=”Name” />
<TextBox Name=”tbUserName” Text=”{Binding ElementName=cbUsers, Path=SelectedValue}” />
When you add UpdateSourceTrigger=Explicit to the TextBox, you can control the updating by telling the binding mechanism to update.
<TextBox Name=”tbUserName” Text=”{Binding ElementName=cbUsers, Path=SelectedValue, UpdateSourceTrigger=Explicit}” />
How do you do that? That’s where the magic lies 
You need to get the BindingExpression for the TextBox:
BindingExpression binding = tbUserName.GetBindingExpression(TextBox.TextProperty);
Then update the linked source( the textbox):
binding.UpdateSource();
bingo
Mar 4 2008 5:56PM GMT
Posted by: Mark Shurmer
WPF
Have you ever wondered how to alter the update frequency of a binding?
Probrably like me, you use the default settings unless you notice something not working
What am I talking about?
Well, when you have a binding defined to another element for example:
<TextBox Name=”txtFirstOne” />
<TextBox Text=”{Binding ElementName=txtFirstOne, Path=Length” Name=”txtFontSize” />
The second textbox will display the length of the text in the first textbox, as you type. Well actually it doesn’t, as the target is not being updated. To fix that you could add UpdateSourceTrigger=PropertyChanged for this example, and bingo:
<TextBox Text=”{Binding ElementName=txtFirstOne, Path=Length, UpdateSourceTrigger=PropertyChanged” Name=”txtFontSize” />