Josh
Josh

Reputation: 10624

How can I bind a querystring to a property on my view model in silverlight?

I have a view model with an ID that I'd like to be able to set from a querystring. Is there a way in XAML to set the value of a property to a querystring value? Here's the current XAML:

<local:DetailsViewModel x:Key="viewModel" DetailsID="1" />

Is there XAML that is effectively something like this?

<local:DetailsViewModel x:Key="viewModel" DetailsID="{Binding HtmlDocument.Querystring["id"]}" />

Upvotes: 1

Views: 515

Answers (1)

ColinE
ColinE

Reputation: 70170

To access the querystring in code you can use the following:

var query = System.Windows.Browser.HtmlPage.Document.QueryString;

As you can see, HtmlPage is a static class. However, binding to static instances is not possible in Silverlight (in WPF you can use {x:Static}).

On another note, most people use regular CLR objects for their ViewModels, rather than DependencyObjects with DependencyProperties, which is thought of as overkill. Therefore, you cannot create a ViewModel with a property that is bound!

Is there any reason why you don't want to do this in code?

Upvotes: 1

Related Questions