Min
Min

Reputation: 11

MVVM - How to show a Popup in DataGrid on MouseOver on a Button in Row

I have the following code where a DataGrid is populating the data from my ViewModel. DataGrid can have multiple rows each row have a Button.

I want to show a popup when user will Mouse over on that button.(Currently my Popup is not working)

I am new to WPF, so please help me by considering this. :)

Thank you very much !!

<Grid DataContext="{StaticResource Windows1ViewModel}">
    <StackPanel Orientation="Vertical"
                Margin="20"
                HorizontalAlignment="Center"
                VerticalAlignment="Top">
        <Button Content="Refresh"
                Style="{StaticResource ButtonStyle1}"
                HorizontalAlignment="Left"
                Command="{Binding RefreshDateCommand}" />
        <TextBlock Text="{Binding MyDateTime}" />
        <Button Name="rndm" Content="Randomize Grid Data"
                HorizontalAlignment="Left"
                Margin="0 20 0 0"
                Command="{Binding RefreshPersonsCommand}" />
        <DataGrid ItemsSource="{Binding PersonsCollection}"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False">
            <DataGrid.Columns>                    
                <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}"
                                    Header="Name (string)" />
                <DataGridTextColumn Binding="{Binding Age}"
                                    IsReadOnly="True"
                                    Header="Age (int)" />
                <DataGridTemplateColumn Header=" "
                                        IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Button Name="btnAdditionalInformation" Content="AI"
                                       Width="40"
                                       Height="20">
                                </Button>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Grid >
                                <Popup x:Name="popUp" 
                            PlacementTarget="{Binding ElementName= 
                                             btnAdditionalInformation}" 
                            IsOpen="True" Width="200" Height="100">
                                    <StackPanel Orientation="Vertical"
                                                Background="Gray" >
                                        <TextBlock Text="asdf"></TextBlock>
                                        <Button Content="button1"/>
                                    </StackPanel>
                                </Popup>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Binding="{Binding IsMarried, Mode=TwoWay,
                                        UpdateSourceTrigger=PropertyChanged}"
                                        Header="IsMarried (bool)" />
                <DataGridTextColumn Binding="{Binding Height, Mode=TwoWay, 
                                    UpdateSourceTrigger=PropertyChanged}"
                                    Header="Height (double)" />
                <DataGridTextColumn Binding="{Binding BirthDate}"
                                    IsReadOnly="True"
                                    Header="BirthDate (DateTime)" />
                <DataGridTemplateColumn Header="FavoriteColor (SolidColorBrush)"
                                        IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid Background="{Binding FavoriteColor}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button Content="can execute command?"
                VerticalAlignment="Top"
                HorizontalAlignment="Left"
                Margin="0 20 0 0"
                Command="{Binding DoNothingCommand}" />
    </StackPanel>

</Grid>

Upvotes: 1

Views: 3400

Answers (1)

Nikita B
Nikita B

Reputation: 3333

Here is a simple example:

<StackPanel>
    <Button x:Name="btn" Content="Button"/>
    <Popup AllowsTransparency="True" Placement="Mouse" IsOpen="{Binding Path=IsMouseOver, ElementName=btn, Mode=OneWay}">
        <Border Background="DarkGray">
            <TextBlock Text="Popup"/>
        </Border>
    </Popup>
</StackPanel>

This will open popup when mouse is over the button and close it, when its not. However if you need that kind of behavior you should consider using a tooltip instead.

Upvotes: 6

Related Questions