我有以下代码,其中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>
答案 0 :(得分: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>
当鼠标悬停在按钮上并关闭它时,这将打开弹出窗口。但是,如果您需要这种行为,则应考虑使用工具提示。