2013-02-06 68 views
0

我的項目中有一個奇怪的問題。有從usercontrol和菜單欄(也usercontrol)製成的頁面。用戶控件中的用戶控件wpf無響應

這裏是我的用戶,它包含幾個按鈕

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訪問我的活動,爲什麼?

+0

是什麼Plc的?我沒有看到你在xaml中命名你的控件 –

+0

我認爲這是'EventbtCliced'是一個錯字,你的意思是:'EventbtClicked'? – CodingGorilla

回答

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,捉事件這種方式是不要做它在所有的最佳途徑。

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

} 

    } 

在用戶控件的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"; 
    } 
} 

}