Home > Back-end >  Launch MouseEnter event Image from XAML dictionnary with MVVM
Launch MouseEnter event Image from XAML dictionnary with MVVM

Time:12-01

I have a template for a DataGrid located in a ResourceDictionary.

BlockStyles.xaml

<Style TargetType="DataGrid" x:Key="SearchExpGrid">
    <Setter Property="AlternatingRowBackground" Value="#4C87C6ff"/>
    <Setter Property="GridLinesVisibility" Value="None"/>
    <Setter Property="FontSize" Value="15"/>
    <Setter Property="HeadersVisibility" Value="Column"/>
    <Setter Property="CellStyle">
        <Setter.Value ... />
    </Setter>
    <Setter Property="RowStyle">
        <Setter.Value ... />
    </Setter>
    <Setter Property="ColumnHeaderStyle">
        <Setter.Value>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border 
                                x:Name="Border" Background="White" BorderBrush="#4C87C6" BorderThickness="1" >
                                <StackPanel Orientation="Horizontal" Margin="5,5" HorizontalAlignment="Center">
                                    <TextBlock 
                                        x:Name="TxtB"  Text="{Binding}"
                                        Foreground="#4C87C6" FontWeight="DemiBold" HorizontalAlignment="Center"/>
                                    <Image Source="../Images/dropdown.png"
                                               Width="10" Height="10" Margin="5,0,5,0"
                                              MouseEnter="DropdownButton_MouseEnter"
                                              MouseLeave="DropdownButton_MouseLeave"
                                              MouseEnter="DropdownButton_Click"/>
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="#4C87C6"/>
                                    <Setter TargetName="TxtB" Property="Foreground" Value="White"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

I always have an error because the function can't find it's definition.

I first implemented the function in the View file where the style is used but it doesn't work.

I tried this method from StackOverflow using a resource class inheriting ResourceDictionnary but got the same error.

I then tried to use ICommand and RelayCommand to execute the function from the ViewModel but didn't got any result.

I also didn't find where I could add an EventHandler ImgDropdownButton.MouseEnter = new MouseEventHandler(MouseEnter_DropdownButton); using MVVM.

Is there a better solution for this kind of behaviour or if adding an EventHandler is the best solution, where sould be the best place to add it ?

Thanks in advance

Edit :

I managed to handle the function using a code-behind file for my ResourceDictionary following this.

BlockStyles.xaml.cs

private void DropdownButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    Mouse.OverrideCursor = Cursors.Hand;
}

private void DropdownButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    Mouse.OverrideCursor = Cursors.Arrow;
}

private void DropdownButton_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    //Function to show the popup
}

The MouseEnter and MouseLeave function are working, but I don't understand how to use the function to make my popup appear.

What I'm trying to do is that when I click on the Dropdown Image on the column header, I want to display a Popup, like an Excel one. This will allow the user to filter the columns values.

The file where my Grid and Popup are : (SearchExpView.xaml)

<Grid Grid.Row="2" Grid.Column="1">
    <searchcomponents:ExpListView x:Name="ExpDatagrid"
        DataContext="{Binding OExpListVM}" 
        Width="auto" Height="auto"/>
</Grid>

<Popup x:Name="PopupFiltre">
    PopupFiltre content
</Popup>

Definition of my Datagrid : (ExpListView.xaml)

<Grid>
    <DataGrid x:Name="ExpGrid" Style="{StaticResource SearchExpGrid}"
                  BorderThickness="0" BorderBrush="#4C87C6"
                  HorizontalAlignment="Left" VerticalAlignment="Top" 
                  MinHeight="200" Height="auto" Margin="10,10,0,0"
                  MinWidth="780" Width="auto"
                  ItemsSource="{Binding}" DataContext="{Binding tableExpertise.DefaultView}"
                  AutoGenerateColumns="True" CanUserAddRows="False" IsReadOnly="True">
        <DataGrid.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick"
                              Command="{Binding DataContext.OnRowDoubleClickedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                              CommandParameter="{Binding ElementName=ExpGrid, Path=CurrentItem}"/>
        </DataGrid.InputBindings>
        <DataGrid.ContextMenu>
            <ContextMenu Name="dgctxmenu">
                <Separator></Separator>
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>
</Grid>

I'm looking for a way to be able to implement this popup fonction but I can't find out how to link everything together.

CodePudding user response:

My Window is SearchExpView.xaml (with the Datagrid and the Popup). My Datagrid component is defined in ExpListView.xaml and styled in BlockStyles.xaml, which is not a window. I want to make the Popup (in SearchExpView.xaml) visible by clicking on the dropdown button (defined in BlockStyles.xaml)

Then you need to get a reference to the Popup in the window from the ResourceDictionary somehow.

You could for example use the static Application.Current.Windows property:

var window = Application.Current.Windows.OfType<SearchExpView>().FirstOrDefault();
if (window != null)
    window.PopupFiltre.IsOpen = true;

Also make sure that you make the Popup accessible from outside the SearchExpView class:

<Popup x:Name="PopupFiltre" x:FieldModifier="internal">
...
  • Related