Is it possible to retrieve data inside an XAML file?

In an application I need to use some custom object where each have their own properties and these properties coming from a database so in

 <Style x:Key="key1" TargetType="Rectangle">

        <Setter Property="Stretch" Value="Fill"/>
        <Setter Property="IsHitTestVisible" Value="False"/>
        <Setter Property="s:Prop.ID" Value="1234567890-ABC" />
    </Style>

I must assign values to the properties. Any help will be appreciated.

Thanks, Amit

Answers


You can use TryFindResource(object key). For example, if your style is under your window's resources and you want to set the second setter to true, you can put this in the code-behind:

((TryFindResource("key1") as Style).Setters[1] as Setter).Value = true;

You could also use binding. Just set the value to Value="{Binding Path=path}" and make your path a dependency property. This also works with multibinding and conversion.


Perhaps MVVM will help you out. Then you have a ViewModel, which exposes your properties to the view. The Model is what the database will deliver. The ViewModel is an adapter between model and view.

Jason Dollinger explains it very good in his video which is available here


Need Your Help