11

I have a ListView with a View Model. The ItemsSource is a collection of objects in the View Model. A property exists on the View Model for some flag, IsFlagOn. I want to set that property in the View Model to True when the ListViewItem detects a IsMouseOver. Other UI elements are then bound to this same property so that the view changes as MouseOver is toggled.

How would I accomplish this in XAML?

I would imagine something like this (but this breaks):

<Style> <!-- on the ListViewItem -->
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="DataContext.IsFlagOn" Value="True" />
        </Trigger>
    </Style.Triggers>
</Style>

UPDATE:

The error is

Cannot resolve the Style Property 'IsFlagOn'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.

UPDATE(2):

Here's a bit more of the existing XAML (following). You can see that the ListView is bound with a property of the VM, AllItems. Important to note that each item in the list is a VM, where each column is bound. So is the ItemContainerStyle binding against the ListView VM or the Item VM?

<ListView Itemssource="{Binding AllItems}">
    <ListView.ItemContainerStyle>
        <Style> <!-- on the ListViewItem -->
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="DataContext.IsFlagOn" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <!-- ... -->
        </GridView>
    </ListView.View>
</ListView>
Mike Caron
  • 5,674
  • 4
  • 48
  • 71
  • `but this breaks` with what exception? Is `IsFlagOn` a dependency property? – Arian Motamedi Aug 27 '13 at 19:24
  • Error is "Type DataContext is not found". View model is plain class implementing `INotifyPropertyChanged`, so `IsFlagOn` is not a dependency property. – Mike Caron Aug 27 '13 at 19:28
  • OK, so what is the actual exception? – Arian Motamedi Aug 27 '13 at 19:29
  • Are you applying this style on the ListView, and yet want the "IsMouseOver" property change to be detected for each individual ListViewItem? The way I see it, this will react whenever the mouse is over the ListView itself, not any particular ListViewItem. Is this what you really want? – Samir Aug 27 '13 at 19:30
  • @Samir see update. It's a style on ListViewItem. – Mike Caron Aug 27 '13 at 19:33
  • @PoweredByOrange see updated comment – Mike Caron Aug 27 '13 at 19:33
  • is `IsFlagOn` a property belonging to the VM or does it belong to each item in your collection? If it's the VM that's prolly the error, Use a RelativeSource Binding with TargetType as ListView to get to the right scope and then you should be able to find the property. – Viv Aug 27 '13 at 19:40
  • @Viv, I updated my question with more of the XAML. The property belongs to each item in the collection. – Mike Caron Aug 27 '13 at 19:48

1 Answers1

8

This is pretty much what OneWayToSource binding mode was made for - being able to just update the view model from the view. However, since IsMouseOver is a read-only property, you won't be able to do this (due to a bug in WPF):

<Setter Property="IsMouseOver" Value="{Binding Path=IsFlagOn, Mode=OneWayToSource}" />

There are ways to get around it though. Some of them are described here: OneWayToSource binding from readonly property in XAML

Community
  • 1
  • 1
Adi Lester
  • 24,731
  • 12
  • 95
  • 110