Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Databinding and Styles with MVVM
    primarykey
    data
    text
    <p>I have a problem with databinding on a style in WPF.</p> <p>The basic setup looks like this:</p> <pre><code>&lt;Style x:Key="{x:Type eo:Player}" TargetType="{x:Type eo:Player}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Team}" Value="A"&gt; &lt;Setter Property="Template" Value="{StaticResource TeamATemplate}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>The style is applied to all objects of type Player. These objects have a property of type Teams (Enum having values A, B and C). Depending on which team the player is in the template applied to visualize the player differs.</p> <p>The problem that now occurs is that the whole thing is used in a MVVM application and that somehow the DataContext of the Player object gets set to the ViewModel of the topmost View. I used the new diagnostics options (TraceLevel) to find out something about the problem and got this:</p> <pre><code>System.Windows.Data Warning: 66 : BindingExpression (hash=30607723): Found data context element: Player (hash=35170261) (OK) System.Windows.Data Warning: 74 : BindingExpression (hash=30607723): Activate with root item ToolboxViewModel (hash=61398511) System.Windows.Data Warning: 104 : BindingExpression (hash=30607723): At level 0 - for ToolboxViewModel.Team found accessor &lt;null&gt; </code></pre> <p>So basically the Player object is found as a data context element (whatever that means) but still the ToolboxViewModel is used as DataContext. How can I fix this? How can I refer to the styled object in the binding expression?</p>
    singulars
    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.
 

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