Febertson
Febertson

Reputation: 474

XAML Set a Resource Key by Binding

I want to Change the Style of a Grid dynamically. For that Purpose let's suppose I have 3 Textblocks with 3 Contents defined.

<TextBlock x:Name="Block1" Text="key1" />
<TextBlock x:Name="Block2" Text="key2" />
<TextBlock x:Name="Block3" Text="key3" />

For each of the keys (1,2,3) there is a Style defined with the Name x:Key="key1". Now i want something like this in my Grid:

<Grid Style="{DynamicResource {Binding ElementName=Block1, Path=Text}} />

Is this possible in Default XAML or do I have to find a Workaround?

Upvotes: 3

Views: 1033

Answers (1)

mm8
mm8

Reputation: 169400

Is this possible in Default XAML or do I have to find a Workaround?

No, I am afraid you cannot use the StaticResource or DynamicResource markup extension with "dynamic" values like this. The keys of the resources must be known at compile time.

Binding to a property and use a converter would be one way:

<Grid Style="{Binding ElementName=Block1, Path=Text, Converter={StaticResource converter}}">

But I guess that's a workaround.

Upvotes: 1

Related Questions