English 中文(简体)
内容目录与Trigger
原标题:Change Template of ContentControl with Trigger

我有一个包含许多项目的清单箱。 当我对某个项目置之不理时,我需要表现出相当强烈的愤怒。 我确信,每个项目的填满是浪费资源,因此,我只想在项目一上对项目一作改动,修改项目内的内容目录,以包括“动.”。 这是我迄今为止所做的事:(简化版)(该法典可留到卡西姆尔)。

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

The problem is that when I try to MouseOver the Popup - it disappears (like a tooltip) because I I m the mouse is leaving the original ControlTemplate - which causes the template with the Popup to disappear. Any ideas? Edit: I have codebehind available as well to achieve this (even though I d prefer xaml)

最佳回答

I have what I think is (at least part of) a solution: Instead of changing the ContentControl s Template via a Trigger, changing the Popup s content with the Trigger seems to work. So instead of loading a complex Popup with a large Visual Tree for each item, I load a simple Popup with no content for each item - and only fill that content when I MouseEnter the ContentControl. Like so:

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

I m not quite sure how to measure how much I would gain performance-wise using this method, but I think it seems to make sense. I d still like to hear any other better ideas. Thanks.

问题回答

你们需要做一点不同的事情——尝试这样的事情:

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




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签