WPF Tips

#1: Using BasedOn Style Property on DynamicResources

I've found that since you can't use BasedOn on a DynamicResource, you can „convert“ the DynamicResource to StaticResource by merging the ResourceDictionary holding your „parent“ resources to your current Window/UserControl/whatever. This way you are now able to refer to the resource object (eg. Style) using StaticResource. This way you can use Datatriggers on DynamicResource (through conversion).


            <ResourceDictionary Source="/MyProject.Styles;component/ButtonStyles.xaml"/>
        [*Your other resources can be put here*]

<Button Command="{Binding MyCommandInViewModel, RelativeSource={RelativeSource AncestorType=Window}}">
        <Style BasedOn="{StaticResource StyleFromButtonStyles}" TargetType="Button">
                <DataTrigger Binding="{Binding SomeBool}" Value="True">
                    <Setter Property="Button.Content" Value="{StaticResource SomeImage}"/>
                <DataTrigger Binding="{Binding SomeBool}" Value="False">
                    <Setter Property="Button.Content" Value="{StaticResource SomeOtherImage}"/>

Hence Datatriggers are applied to a button styled in an imported ResourceDictionary.

#2: How to fire a command on double-click listbox item?

The listbox itself is reacting to the double click, but only in the part of it's area that is not covered by a list box item.

You need the event handler to be tied to the listboxitem.

public class ControlItemDoubleClick : DependencyObject {
public ControlItemDoubleClick()
public static readonly DependencyProperty ItemsDoubleClickProperty =
	typeof(bool), typeof(Binding));
public static void SetItemsDoubleClick(ItemsControl element, bool value)
	element.SetValue(ItemsDoubleClickProperty, value);
	if (value)
		element.PreviewMouseDoubleClick += new MouseButtonEventHandler(element_PreviewMouseDoubleClick);
static void element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
	ItemsControl control = sender as ItemsControl;
	foreach (InputBinding b in control.InputBindings)
		if (!(b is MouseBinding))
		if (b.Gesture != null
			&& b.Gesture is MouseGesture
			&& ((MouseGesture)b.Gesture).MouseAction == MouseAction.LeftDoubleClick
			&& b.Command.CanExecute(null))
			e.Handled = true;
public static bool GetItemsDoubleClick(ItemsControl element)
	return (bool)element.GetValue(ItemsDoubleClickProperty);
<ListBox Name="SmallVideoPreviews" Grid.Column="1" MaxHeight="965"
    ItemsSource="{Binding BrowseVideos}" 
    ItemTemplate="{StaticResource BrowseTemplate}">
        <KeyBinding Key="p" 
            Command="{Binding PlayVideoCommand}"
            CommandParameter="{Binding ElementName=SmallVideoPreviews, Path=SelectedItem}"/>
        <MouseBinding Gesture="LeftDoubleClick"
            Command="{Binding PlayVideoCommand}"
            CommandParameter="{Binding ElementName=SmallVideoPreviews, Path=SelectedItem}"/>


You can add a style to ListBox.ItemContainerStyle, and add an EventSetter there:

        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>

ListBoxItem_MouseDoubleClick is a method in your code behind with the correct signature for MouseDoubleClick.

programovani/wpf/tips.txt · Poslední úprava: 29.03.2015 19:48 autor: vm

Nástroje pro stránku