事件触发器无法在Code Behind

时间:2015-05-11 09:17:03

标签: c# wpf xaml

我想动态地在运行时创建WPF控件。在下面的代码中,我在XAML中添加了StackPanel,并在代码后面添加了Button / TextBlock / Triggers。我想在TextBlock操作的按钮下看到MouseEnter,并希望在TextBlock操作时隐藏MouseLeave。 这没有按预期工作。 如果在XAML中添加Button / Textblock代码(现在注释)而不是在Code后面创建它们(现在未注释),那么它工作正常。 请让我知道如何解决这个问题?不能理解为什么这不起作用如果我在运行时创建按钮而不是XAML代码。感谢

XAML

<Grid>
   <StackPanel Name="stackPanel5" >
      <!--
      <Button Name="Button1" Cursor="Hand" Content="Press Me"/>
      <Border x:Name="TooltipBrd" BorderBrush="Black" BorderThickness="10,0,10,10" HorizontalAlignment="Center"  VerticalAlignment="Center"  Visibility="Collapsed">
         <TextBlock Margin="0,10,0,0" x:Name="myTextBlock" Text="Coded Test" />
      </Border>
      -->
   </StackPanel>
</Grid>

public void LoadData()
{
   //VisualState vs = new VisualState();
   //vs.SetValue(FrameworkElement.NameProperty, YourStateName);

   Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!" };
   stackPanel5.Children.Add(Button1);

   Border TooltipBrd = new Border()
   {
       BorderBrush = Brushes.Black,
       BorderThickness = new Thickness(10, 10, 10, 10),
       HorizontalAlignment = HorizontalAlignment.Center,
       VerticalAlignment = VerticalAlignment.Center,
       Visibility = Visibility.Collapsed
   };

   TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
   TooltipBrd.Child = txtB1;

   stackPanel5.Children.Add(TooltipBrd);

   System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
   trigger1.EventName = "MouseEnter";

   ChangePropertyAction action1 = new ChangePropertyAction();
   action1.TargetName = "TooltipBrd";
   action1.PropertyName = "Visibility";
   action1.Value = Visibility.Visible;

   trigger1.Actions.Add(action1);
   trigger1.Attach(Button1);

   System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
   trigger2.EventName = "MouseLeave";

   ChangePropertyAction action2 = new ChangePropertyAction();
   action2.TargetName = "TooltipBrd";
   action2.PropertyName = "Visibility";
   action2.Value = Visibility.Collapsed;

   trigger2.Actions.Add(action2);
   trigger2.Attach(Button1);
}

2 个答案:

答案 0 :(得分:0)

在XAML上,创建像这样的事件

<Button x:Name="sample_btn" MouseEnter="sample_btn_MouseEnter" />

现在在后面的代码中,

private void logOut_btn_MouseEnter(object sender, MouseEventArgs e)
{
   //your logic on mouse enter
}

同样地,对于鼠标离开,你很高兴。

答案 1 :(得分:0)

    Magnus(MM8) - Given beautiful solution for this.
    https://social.msdn.microsoft.com/profile/magnus%20(mm8)/?ws=usercard-mini 

    **Just set the TargetObject property of the actions to TooltipBrd and your code will work:**

           public void LoadData() {
              //VisualState vs = new VisualState();
              //vs.SetValue(FrameworkElement.NameProperty, YourStateName);

              Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!" };
              stackPanel5.Children.Add(Button1);

              Border TooltipBrd = new Border() {
                BorderBrush = Brushes.Black,
                BorderThickness = new Thickness(10, 10, 10, 10),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Visibility = Visibility.Collapsed
              };

              TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
              TooltipBrd.Child = txtB1;

              stackPanel5.Children.Add(TooltipBrd);

              System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
              trigger1.EventName = "MouseEnter";

              ChangePropertyAction action1 = new ChangePropertyAction();
              action1.**TargetObject** = TooltipBrd;
              action1.PropertyName = "Visibility";
              action1.Value = Visibility.Visible;

              trigger1.Actions.Add(action1);
              trigger1.Attach(Button1);

              System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
              trigger2.EventName = "MouseLeave";

              ChangePropertyAction action2 = new ChangePropertyAction();
              action2.**TargetObject** = TooltipBrd;
              action2.PropertyName = "Visibility";
              action2.Value = Visibility.Collapsed;

              trigger2.Actions.Add(action2);
              trigger2.Attach(Button1);
            }

    **If you set the TargetName property you need to call the RegisterName method on the StackPanel:**
    int i = 0;
public void LoadData()
        {
            ++i;
            //VisualState vs = new VisualState();
            //vs.SetValue(FrameworkElement.NameProperty, YourStateName);

            Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!", Height = 150, Width=150 };
            stackPanel5.Children.Add(Button1);

            Border TooltipBrd = new Border()
            {
                BorderBrush = Brushes.Black,
                BorderThickness = new Thickness(10, 10, 10, 10),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Visibility = Visibility.Collapsed
            };

            TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
            TooltipBrd.Child = txtB1;

            stackPanel5.Children.Add(TooltipBrd);

            System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
            trigger1.EventName = "MouseEnter";

            ChangePropertyAction action1 = new ChangePropertyAction();
            //action1.TargetObject = TooltipBrd;
            action1.TargetName = "TooltipBrd"+i;
            action1.PropertyName = "Visibility";
            action1.Value = Visibility.Visible;

            trigger1.Actions.Add(action1);
            trigger1.Attach(Button1);

            System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
            trigger2.EventName = "MouseLeave";

            ChangePropertyAction action2 = new ChangePropertyAction();
            action2.TargetName = "TooltipBrd"+i;
            //action2.TargetObject = TooltipBrd;
            action2.PropertyName = "Visibility";
            action2.Value = Visibility.Collapsed;

            trigger2.Actions.Add(action2);
            trigger2.Attach(Button1);
            **stackPanel5.RegisterName("TooltipBrd"+i, TooltipBrd);**
        }    
    When you are creating controls dynamically, you have to call the RegisterName method for the runtime or you to be able to find them using this name.