Wednesday, 3 September 2008

WPF: use IValueConverter to implement dynamic binding to an XML data source ...

Here are some swift details on how to use IValueConverter to implement dynamic binding to an XML data source.

 

Add the XML source to your XAML page/window as a resource. Also add your URL converter that gives you customises your input. You could use XLINQ here to get some nice thingie going. I'm using it for manipulation of a static application map:

 

<Page.Resources>

<XmlDataProvider x:Key="ApplicationMap" Source="ApplicationMap.xml"/>

<src:PageURLConverter x:Key="pageURLConverter"/>

</Page.Resources>

 

Make yourself a Frame and use the XML source and the converter to build the URL that the Frame will hold:

 

<Frame

Name="frameContent"

Source="{Binding XPath=@PageLink, Converter={StaticResource pageURLConverter}}"

NavigationUIVisibility="Hidden"

Focusable="False">                   

</Frame>

 

The converter looks like this:

 

public class PageURLConverter : IValueConverter

{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

        string url = (string)value;

        if (url.StartsWith("http://")) return YourURLManipulateMethod(url);

        else return url;

    }

 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

        return value;

    }

}

 

I used this method in a CRM-type application to whack an Account Code on the end of a URL query string. Allowed me to load a pre-existing ASP.NET component of the CRM into a Frame in the WPF app for the same account the user was working with.

 

No comments:

Post a Comment