Note that there are some explanatory texts on larger screens.

plurals
  1. POOwn DataTemplateSelector MVVM
    primarykey
    data
    text
    <p>I'm using a MVVM-Pattern with a ModelView-First approach. This works fine, so far. Now I have a UserControl (View) which should display various content depending on a Property located in my ViewModel.</p> <p>First, I tried to solve the issue with DataTemplates and a DataTemplateSelector (<a href="http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector" rel="nofollow">See this tutorial</a>) This was working very well. But I was not happy with the solution, because then I have a class (the overrided DataTemplateSelector) which is not connected to the ViewModel and can't be filled from the model.</p> <p>So I tried to create a own TemplateSelector which uses a Property from the ViewModel. Unfortunately the DataTrigger is not triggering. The Binding from a CheckBox to the ViewModel is also working but not at the DataTrigger (even the designer can't find this path).</p> <p>Ok, please have a look at the code:</p> <pre><code>&lt;UserControl.Resources&gt; &lt;!--Define Template which is displayed for Users--&gt; &lt;DataTemplate x:Key="templateUser"&gt; &lt;Image Name="logo" Source="blanked out" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/DataTemplate&gt; &lt;!--Define Template which is displayed for Administrators--&gt; &lt;DataTemplate x:Key="templateAdmin"&gt; &lt;TextBlock Background="Yellow" Margin="3" Text="YEAH, I'm an Administrator" /&gt; &lt;/DataTemplate&gt; &lt;!--My own TemplateSelectpr--&gt; &lt;DataTemplate x:Key="myTemplateSelector"&gt; &lt;ContentControl x:Name="DynamicContent" ContentTemplate="{StaticResource templateUser}"/&gt; &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsAdministrator}" Value="true"&gt; &lt;Setter TargetName="DynamicContent" Property="ContentTemplate" Value="{StaticResource templateAdmin}" /&gt; &lt;/DataTrigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;ContentPresenter ContentTemplate="{StaticResource myTemplateSelector}"/&gt; &lt;/Grid&gt; </code></pre> <p>Of course, I can seperate the Task in two further contentcontrols, but I don't want to maintain those if same content is intersecting. So can someone suggest anything?</p> <p>Best regards, and thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload