WPF - hovering over one element shows content in another element

I would like to get an effect thanks to which I can move the mouse cursor over the Button and update its contents via TextBlock (via binding). To complicate matters, a button is one of many buttons defined in ItemsControl / DataTemplate. TextBlock goes beyond ItemsControl.

Some simplified markup of the problem is as follows:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
  </Grid.RowDefinitions>
  <ItemsControl Grid.Row="0">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Button Content="{Binding Title}"
                Command="{Binding Command}" />    
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
  <TextBlock x:Name="TitleTextBox" Grid.Row="1" />
</Grid>

Say in this example, I can bind the Title property to the data item to the TextBlock property “Text”.

I assume that I want to click on the IsMouseOver button, but I cannot connect it correctly.

+3
source share
1 answer

, - ... codebehind. "" ( , Blend Behavior). , :

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
  </Grid.RowDefinitions>
  <ItemsControl x:Name="Buttons" Grid.Row="0" ext:Hover.TrackChildItems="true">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Button Content="{Binding Title}"
                Command="{Binding Command}" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
  <TextBlock x:Name="TitleTextBox"
             Grid.Row="1"
             Text="{Binding ElementName=Buttons, Path=ext:Hover.Item.Content}" />
</Grid>

"Hover.TrackChildItems" readoverly "Hover.Item" . .

. , , , : Items .

Mouse.AddMouseEnter(item, OnMouseEnter);

, ( ) . , Items.

((INotifyCollectionChanged)Items).CollectionChanged += OnItemsChanged;

, CollectionChanged.

. HoverTrackingItemsControl , ItemsControl. GetContainerForItemOverride HoverTrackingItem, MouseEnter/MouseLeave HoverTrackingItemsControl. , , , , Behavior ItemsControl (ItemsControl, ListBox, ComboBox ..).

+4

Source: https://habr.com/ru/post/1707009/


All Articles