How to bind with dynamic resource and specifying a path
Asked Answered
A

2

12

I want to bind to a resource (DynamicResource) and access properties on that resource, but is there a way to do that?

(I want to visualize the default values from constructor in the xaml editor in visual studio. Those cannot be seen when referencing an object through DataContext nor through a property added on my Window class...)

Not working xaml: (works in composer but not at runtime...)

<Window ... >
    <Window.Resources>
        <local:MyClass x:Key="myResource"  />
    </Window.Resources>
    <StackPanel>
        <Button Content="{Binding Source={DynamicResource myResource} Path=Property1}" />
        <Button Content="{Binding Source={DynamicResource myResource} Path=Property2}" />
    </StackPanel>
</Window>

with the class (which probably need to implement INotifyPropertyChanged):

public class MyClass 
{
    public MyClass()
    {
        this.Property1 = "Ok";
        this.Property2 = "Cancel";
    }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}
Adobe answered 30/8, 2010 at 9:8 Comment(0)
H
28

That's because the DynamicResource markup extension can only be used on a dependency property, because it will need to update it if the resource changes. And Binding.Source is not a dependency property...

As a workaround, you could set the DataContext of the button with the DynamicResource :

<Button DataContext="{DynamicResource myResource}" Content="{Binding Path=Property1}" />
<Button DataContext="{DynamicResource myResource}" Content="{Binding Path=Property2}" />
Highup answered 30/8, 2010 at 9:17 Comment(2)
+1, this seems to be a cleaner work around and I've done this to good effectJobe
Can anyone explain why this will only tend to work after the parent windows Loaded event has fired? Even though using the code behind api etc TryFindResource() it will return the expected resource?Lotuseater
J
2

Abusing the DataContext of an unrelated object seems to be the easiest workaround. In case you still need the DataContext of your control (MVVM anyone?), you can also create an invisible helper FrameworkElement elsewhere:

<FrameworkElement Visibility="Collapsed" x:Name="ControlBrushGetter"  DataContext=" 
{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

and later refer to it by using the name in the binding:

<SolidColorBrush Opacity="0.8" 
Color="{Binding ElementName=ControlBrushGetter, Path=DataContext.Color}" />

Your designer will quite likely complain about not being able to resolve "Color" in the context of "object", but it will work fine at runtime.

Jointworm answered 12/11, 2018 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.