John
John

Reputation: 203

Access the ressource of the View from the viewmodel WPF MVVM

I have a View with a DataTemplate

<UserControl.Resources>
        <DataTemplate x:Key="TrackChangeTemplate">
                    <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"  />
        </DataTemplate>
    </UserControl.Resources>

and i want in the View :

 SimpleLink link = new SimpleLink();
    link.Detail = (DataTemplate)Resources["TrackChangeTemplate"];
    link.DetailCount = 1;
    link.CreateDocument(true);

However i can't access the Resouces : (DataTemplate)Resources["TrackChangeTemplate"]; Resources is not known

John

Upvotes: 5

Views: 7749

Answers (1)

Lukasz Madon
Lukasz Madon

Reputation: 14994

try 
{
  object resource = Application.Current.FindResource("TrackChangeTemplate");
}
catch (ResourceReferenceKeyNotFoundException ex) 
{
 ///stuff here to hande
}

Upvotes: 15

Related Questions