Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there anyway of consolidating similar data bindings and/or triggers in XAML?
    text
    copied!<p>I have a user control that hosts other controls. The way I implemented this is via data templates that define the control that should be associated with a specific view-model. These view-models have similar properties and interaction triggers. Please see XAML snippet below.</p> <p>The problem with this approach is that I would have to copy-paste the data bindings if I want to support a new view-model. Is there any way of consolidating all similar data bindings and/or triggers into one template? I don't want to type/copy-paste the same data binding definitions into each control. (Yes, I know, I'm that lazy.)</p> <pre><code>&lt;UserControl.Resources&gt; &lt;DataTemplate DataType="{x:Type vm:SomeViewModel1}"&gt; &lt;TextBlock Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" RenderTransform="{Binding Transform}" Height="{Binding Height}" Width="{Binding Width}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="MouseEnter"&gt; &lt;cmd:EventToCommand Command="{Binding MouseEnterCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;i:EventTrigger EventName="MouseLeave"&gt; &lt;cmd:EventToCommand Command="{Binding MouseLeaveCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:SomeViewModel2}"&gt; &lt;Rectangle Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" RenderTransform="{Binding Transform}" Height="{Binding Height}" Width="{Binding Width}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="MouseEnter"&gt; &lt;cmd:EventToCommand Command="{Binding MouseEnterCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;i:EventTrigger EventName="MouseLeave"&gt; &lt;cmd:EventToCommand Command="{Binding MouseLeaveCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/Rectangle&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:SomeViewModel3}"&gt; &lt;Button Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" RenderTransform="{Binding Transform}" Height="{Binding Height}" Width="{Binding Width}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="MouseEnter"&gt; &lt;cmd:EventToCommand Command="{Binding MouseEnterCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;i:EventTrigger EventName="MouseLeave"&gt; &lt;cmd:EventToCommand Command="{Binding MouseLeaveCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/Button&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:SomeViewModel4}"&gt; &lt;!-- Do not want copy-paste code here... --&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; </code></pre>
 

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