wpf usercontrol中的Usercontrol无响应

时间:2013-02-06 11:33:22

标签: c# wpf

我的项目中有一个奇怪的问题。有来自usercontrol和菜单栏(也是usercontrol)的页面。

这是我的usercontrol,包含几个按钮

public partial class UpperBar : UserControl
{       
    public UpperBar()
    {
        InitializeComponent();
    }

    public event EventHandler EventbtClicked;
    private void btConnect_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        EventbtClicked(this, e);
    }    
}

我在页面中添加了以下内容:

<local:UpperBar VerticalAlignment="Top" Grid.Row="0" Height="78" Grid.ColumnSpan="3" Margin="0,2,0,0"/>

在我的页面中试图调用事件:

public PageStatus()
{
    InitializeComponent();
    Plc.ExecuteRefresh += new EventHandler(RefreshLeds);


   UpperBar.EventbtCliced += new EventHandler(UpperBatButtonClick);  
}

protected void UpperBarButtonClick(object sender, EventArgs e)
{
    //do something
}

但我无法使用此UpperBar.EventbtCliced访问我的活动,为什么?

2 个答案:

答案 0 :(得分:2)

您需要在PageStatus中访问类UpperBar的实例,而不是UpperBar类本身!

这里最简单的方法是:

  • 在XAML中命名您的UpperBar,例如:

<local:UpperBar x:Name="_myBar" x:FieldModifier="private"/>

  • 然后在PageStatus.xaml.cs中使用此实例:

    public partial class MainWindow : Window    {
    public MainWindow()
    {
        InitializeComponent();
    
        _myBar.EventbtClicked += new EventHandler(UpperBarButtonClick);
    }
    
    protected void UpperBarButtonClick(object sender, EventArgs e)
    {
        //do something
    }
    

    }

现在,如果你在WPF中认真工作,你应该真正了解数据绑定和MVVM,以这种方式捕获事件并不是最好的方法。

答案 1 :(得分:0)

您应该使用自定义命令(RoutedUICommand)而不是来自用户控件的冒泡事件。

以下是与您的方法形成对比的一些步骤:

1:创建类myCustomCommand。

      namespace WpfApplication1

      {

         public  class myCustomCommand.
          {

               private static RoutedUICommand _luanchcommand;//mvvm

                 static myCustomCommand.()
                  {
               System.Windows.MessageBox.Show("from contructor"); // static consructor is                                                 called when static memeber is first accessed(non intanciated object)
       InputGestureCollection gesturecollection = new InputGestureCollection();
       gesturecollection.Add(new KeyGesture(Key.L,ModifierKeys.Control));//ctrl+L
       _luanchcommand =new RoutedUICommand("Launch","Launch",typeof(myCustomCommand.),gesturecollection);

   }
   public static  RoutedUICommand Launch
   {
       get
       {
           return _luanchcommand;
       }
   }

}

   }

在UserControl的xaml中:

                    <UserControl x:Class="WpfApplication1.UserControl1"

         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

          xmlns:CustomCommands="clr-namespace:WpfApplication1"

         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

                <UserControl.CommandBindings> 
              <CommandBinding Command="CustomCommands:myCustomCommand.Launch" Executed="CommandBinding_Executed">

    </CommandBinding>
</UserControl.CommandBindings>
<Grid >
    <TextBox  Name="mytxt"   Height="30" Width="60" Margin="50,50,50,50" ></TextBox>
    <Button Name="b" Height="30" Width="60" Margin="109,152,109,78" Command="CustomCommands:ZenabUICommand.Launch"></Button>

</Grid>

现在在用户控制代码中

处理command_executed

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        mytxt.Text = "invoked on custom command";
    }
}

}