WPF:IsMouseOver触发器不适用于datatemplate

时间:2014-10-31 16:45:10

标签: wpf

我试图让IsMouseOver触发器适用于datatemplate。由于某种原因,它没有开火。我也尝试了http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html,但我没有看到任何痕迹。这是代码:

XAML:

<Window x:Class="FirstSImpleDataApp.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window4" Height="300" Width="300">
    <Window.Resources>
    <ResourceDictionary>
            <DataTemplate x:Key="tmptemplate">
                <Border x:Name="brd" BorderBrush="Black" BorderThickness="2">
                    <TextBlock x:Name="txt">my text box</TextBlock>
                </Border>
                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="txt" Property="Background" Value="Red"></Setter>
                        <Setter TargetName="txt" Property="Foreground" Value="Green"></Setter>
                        <Setter TargetName="brd" Property="Background" Value="Green"></Setter>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>



        </ResourceDictionary>
    </Window.Resources>
    <Canvas x:Name="can" Loaded="can_Loaded">

    </Canvas>
</Window>

代码背后:

   public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
        }

        private void can_Loaded(object sender, RoutedEventArgs e)
        {
            var tmp = this.TryFindResource("tmptemplate") as DataTemplate;
            var obj = (FrameworkElement)tmp.LoadContent();
            can.Children.Add(obj);
        }
    }

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

你的触发器实际上工作正常。问题在于您如何实例化模板并以编程方式将其添加到Canvas

直接在您的Xaml中应用模板,看它是否有效:

<Canvas x:Name="can">
  <ContentControl ContentTemplate="{StaticResource tmptemplate}" />
</Canvas>

如果您想以编程方式应用它,请将模板应用于ContentControlContentPresenter,然后将其放在Canvas中:

private void can_Loaded(object sender, RoutedEventArgs e)
{
    var tmp = this.TryFindResource("tmptemplate") as DataTemplate;
    can.Children.Add(new ContentPresenter { ContentTemplate = tmp });
}

答案 1 :(得分:0)

这是使用Style触发器的替代方法。边框保持黑色,但TextBlock带有绿色边距。

           <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="Foreground" Value="Green"/>
                        <Setter Property="Margin" Value="2"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

            <Style TargetType="Border">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Green"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

          <Border BorderThickness="1" BorderBrush="Black">
               <TextBlock Text="My Text Block"/>
          </Border>