1

我有以下代码,其中 DataGrid 正在从我的 ViewModel 填充数据。DataGrid 可以有多行,每行都有一个Button。

当用户将鼠标悬停在该按钮上时,我想显示一个弹出窗口。(目前我的弹出窗口不起作用)

我是 WPF 的新手,所以请通过考虑这一点来帮助我。:)

非常感谢 !!

<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>

4

1 回答 1

6

这是一个简单的例子:

<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>

当鼠标悬停在按钮上时,这将打开弹出窗口并在没有时关闭它。但是,如果您需要这种行为,您应该考虑使用工具提示。

于 2013-04-27T05:38:09.177 回答