I have a ComboBox
which I need to use in several places in my application, so I set most of the properties of that ComboBox
in ResourceDictionary
and use that as a Style where ever I need it.
Style for the ComboBox
is:
<Style TargetType="{x:Type ComboBox}" x:Key="ComboBoxBranch">
<Setter Property="ItemsSource" Value="{Binding Branches}"></Setter>
<Setter Property="DisplayMemberPath" Value="BranchName"></Setter>
<Setter Property="SelectedItem" Value="{Binding SelectedBranch}"></Setter>
</Style>
and I am using it like this in my XAML:
<ComboBox Style="{StaticResource ComboBoxBranch}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectCustomerCommand}" CommandParameter="{Binding SelectedBranch}" ></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
I want to move the interaction trigger code as well to ResourceDictionary
, so I don't need to write it in all my xamls. Is it possible somehow?
I usually work with Silverlight so I'm not sure if the following approach is sensible in WPF: You can pull your xaml into a
UserControl
, sayBranchSelection.xaml
for example:And use it like this:
As far as I know,
Interaction.Triggers
can not be applied in Style, respectively and in a ResourceDictionary. But you can do so, to determine theComboBox
as a resource withx:Shared="False"
and reference it forContentControl
like this:When
x:Shared="True"
by default then one Style is common to all - in this case, the system swears on the duplicateContent
. Whenx:Shared="False"
when is created Style for each element whenever it its request. Quote fromMSDN
:For more information, please see:
MSDN: x:Shared Attribute
Edit: alternative solution
Here
, Mr.Vspivak published a solution that allows you easily set theInteraction.Triggers
in Style.Example:
MainWindow.xaml
InteractivityHelper.cs
FlipOnHover.cs
ViewModel.cs
For more info, see this link:
Using Interactivity Behaviors and Actions in WPF/Silverlight Styles