【问题标题】:Event Triggers not working in Code Behind事件触发器在代码隐藏中不起作用
【发布时间】:2015-05-11 09:17:03
【问题描述】:

我想在运行时动态创建 WPF 控件。在下面的代码中,我在 XAML 中添加了StackPanel,并在后面的代码中添加了Button/TextBlock/Triggers。我想在MouseEnter 操作的按钮下看到TextBlock,并想在MouseLeave 操作隐藏TextBlock。 这没有按预期工作。 如果在 XAML 中添加 Button/Textblock 代码(现在注释)而不是在后面的代码中创建它们(现在未注释),那么它工作正常。 请让我知道如何解决这个问题?如果我在运行时创建按钮而不是 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);
}

【问题讨论】:

  • 无论你想做什么,删除所有这些并使用正确的 DataBinding。无需在 WPF 的程序代码中创建 UI 元素。使用 ItemsControl。

标签: c# wpf xaml


【解决方案1】:

在 XAML 中,像这样创建事件

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

现在在后面的代码中,

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

同样,为鼠标离开做准备,你就可以走了。

【讨论】:

    【解决方案2】:
        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.
    

    【讨论】:

    • 这违背了 WPF 中所有众所周知的、公认的良好实践和模式。 -1.
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多