2010-05-03 59 views
7

我有WPF用戶控件中的2個文本框和WPF窗體上的按鈕。如何訪問對主窗體的按鈕單擊事件,我現在用的是WPF用戶控件訪問WPF用戶控制值

回答

8

首先這些文本框的值,請記住,WPF不是的WinForms - 理論上你應該數據綁定您的TextBoxes屬性,然後更改屬性的值,而不是直接訪問TextBoxes!

話雖這麼說,所有你需要做的就是命名的用戶控件和文本框,然後訪問它們,就像這樣:

詮釋MyUserControl.xaml

<TextBox x:Name="myTextBox1"/> 
<TextBox x:Name="myTextBox2"/> 

MyWindow.xaml

<local:MyUserControl x:Name="myUserControlInstance"/> 
<Button Content="Click me" Click="Button_Click" /> 

MyWindo w.xaml.cs

private void Button_Click(object sender, RoutedEventArgs e) { 
    myUserControlInstance.myTextBox1.Text = "Foo"; 
    myUserControlInstance.myTextBox2.Text = "Bar"; 
} 
2

在用戶控件,使得它返回一個字符串兩個公共屬性:

public property Textbox1Text 
{ 
    get { return TextBox1.Text; } 
} 

然後從文本框控件將文本的主要形式是可見的。

或者更好的選擇:有一個usercontrol可以引發的事件,稱之爲TextChanged。當然,你想要一個更好的名字爲它比,所以我們就假裝你的第一個文本框是專爲用戶輸入名稱和調用事件NameTextChanged,那麼你的事件將是這樣的:

public MainWindow() 
{ 
    InitializeComponent(); 
    TextBox1.TextChanged += new TextChangedEventHandler(TextBox1_TextChanged); 
} 

private void TextBox1_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    if (NameTextChanged!= null) 
     this.NameTextChanged(this, e); 
} 

public event TextChangedEventHandler NameTextChanged; 

或更好,你可以參加一個路由事件 - 但堅持基礎知識。

0

訂閱一個事件似乎是一個更好的選擇,如上面的slugster所建議的。如果你使用這種方法,你可以在同一個窗口中擁有同一個用戶控件的多個實例,但是根據它們來自哪個用戶控件來處理它們。

作爲一個例子,您可以擁有地址類型的用戶控件,它可以包含發件人地址和收件人地址,這些地址可能具有像街道,城市,州等相同的字段。但發件人地址或郵件地址更新時可能會有不同的行爲。

希望這會有所幫助。

Nilesh製作Gule
http://nileshgule.blogspot.com

0

爲了您的具體問題,我可以建議你一個具體的解決方案。這不能被視爲一般。

您的問題是閱讀您的用戶控件中的文本框的內容按鈕單擊。

以下是解決方案。

在此解決方案中,將會有兩個xaml文件及其相應的.cs文件。

邏輯: - 固有的邏輯是遍歷用戶控件中的視覺效果,找到文本框,在按鈕單擊時讀取文本。

所以這裏是代碼...

  1. Window.xaml - 這是我們的主窗口。這包含1個按鈕和用戶控件的對象引用。

    <Grid> 
        <StackPanel Orientation="Vertical"> 
         <Button x:Name="clickThis" 
           Height="30" 
           Width="70" 
           Content="Click Me!!" 
           Click="clickThis_Click" /> 
         <local:TxtBoxedUC x:Name="UC" /> 
        </StackPanel> 
    </Grid> 
    
  2. TxtBoxedUC.x​​aml - 這是我們的用戶控件。這包含我們的兩個文本框。

    <Grid>   
        <StackPanel Orientation="Vertical"> 
         <TextBox x:Name="txt1" 
           Width="150" 
           Height="30" /> 
         <TextBox x:Name="txt2" 
           Width="150" 
           Height="30" /> 
        </StackPanel> 
    </Grid> 
    
  3. Window1.xaml.cs - 這包含按鈕點擊方法以及通過在用戶控制的視覺元素迭代方法。

    private void clickThis_Click(object sender, RoutedEventArgs e) 
    {    
        GetVisual(UC); 
    } 
    

上面的代碼以處理按鈕點擊。

private void GetVisual(UIElement currentVisual) 
    { 
     int count = VisualTreeHelper.GetChildrenCount(currentVisual); 
     if (count > 0) 
     { 
      for (int i = 0; i < count; i++) 
      { 
       UIElement uiElement = VisualTreeHelper.GetChild(currentVisual, i) as UIElement; 
       if (uiElement != null) 
       { 
        if (uiElement.GetType() == typeof(TextBox)) 
        { 
         TextBox txt = uiElement as TextBox; 
         MessageBox.Show(txt.Text); 

        } 
       } 
       GetVisual(uiElement); 
      } 
     } 
    } 

上面的代碼是遍歷用戶控件中的可視元素。

用戶控件的.cs文件不需要。

現在,當你點擊按鈕,你可以看到你已經進入了MessageBox。

快樂編碼...

請標記爲答案,如果這可以解決您的問題。

0

試試這個:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    string myTextboxValue = this.tbInput.Text; 
}