我有一个包含许多项目的列表框。当我将鼠标悬停在某个项目上时,我需要显示一个相当“重”的弹出窗口。我很确定加载每个项目的弹出窗口是浪费资源所以我想要的只是当我将鼠标悬停在项目上时,我修改了项目中ContentControl的模板以包含Popup。这是我到目前为止:(简化版)(此代码可以粘贴到Kaxaml中)
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="WithPopup" TargetType="ContentControl">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" Name="Target" />
<Popup PlacementTarget="{Binding ElementName=Target}" IsOpen="True" >
<Border BorderBrush="Red" BorderThickness="1" Background="Pink">
<TextBlock Text="I'd like this to behave like a Popup - not a tooltip!" Margin="10" />
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Page.Resources>
<Grid Height="20" Margin="50,50,0,0" Name="ParentGrid">
<ContentControl>
<TextBlock x:Name="TargetControl" Text="Hover over me!" />
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=TargetControl}" Value="True">
<Setter Property="Template" Value="{StaticResource WithPopup}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
</Page>
问题在于,当我尝试使用PopO上的MouseOver时 - 它会消失(就像工具提示一样),因为我是鼠标离开原始的ControlTemplate - 这会导致弹出的模板消失。有任何想法吗? 编辑:我也可以使用代码隐藏来实现这一目标(即使我更喜欢xaml)
答案 0 :(得分:1)
我认为(至少是部分)解决方案是: 而不是通过触发器更改ContentControl的模板,更改弹出窗口 触发器的内容似乎有效。因此,我没有为每个项目加载具有大型Visual Tree的复杂Popup,而是为每个项目加载一个没有内容的简单Popup - 并且只在I MouseEnter ContentControl时填充该内容。像这样:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<DataTemplate x:Key="popupContent" DataType="{x:Type ContentControl}">
<Border BorderBrush="Red" BorderThickness="1" Background="Pink">
<TextBlock Text="This is behaving like a Popup now!" Margin="10" />
</Border>
</DataTemplate>
<ControlTemplate x:Key="WithPopup" TargetType="ContentControl">
<Grid Name="popupGrid">
<TextBlock Text="Hover over me!" />
<Popup IsOpen="True" >
<ContentControl Name="content" />
</Popup>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=popupGrid}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource popupContent}" TargetName="content" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Page.Resources>
<Grid Height="20" Width="100" HorizontalAlignment="Left" Margin="50,50,0,0">
<ContentControl Template="{StaticResource WithPopup}" />
</Grid>
</Page>
我不太确定如何衡量使用这种方法获得多少性能,但我认为它似乎有意义。我还是想听听其他更好的想法。 感谢。
答案 1 :(得分:0)
你需要做一些不同的事情 - 尝试这样的事情:
<ContentControl Content="hover over me!">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock x:Name="TargetControl" Text="{TemplateBinding Content}" />
<Popup PlacementTarget="{Binding ElementName=Target}" x:Name="popup">
<Border BorderBrush="Red" BorderThickness="1" Background="Pink">
<TextBlock Text="I'd like this to behave like a Popup - not a tooltip!" Margin="10" />
</Border>
</Popup>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=TargetControl}" Value="True">
<Setter TargetName="popup" Property="IsOpen" Value="true" />
</DataTrigger>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=popup}" Value="true">
<Setter TargetName="popup" Property="IsOpen" Value="true" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>