2012-03-02 71 views
1

我創建了一個相當簡單的UserControl,它由TextBoxComboBox組成。訪問自定義控件中多個控件的文本和內容

<StackPanel Orientation="Horizontal">   
    <MyNamespace:MultiBox Style="{StaticResource PhoneBoxStyle}" BoxType="Phone" Grid.Column="0" Grid.Row="0" Name="phoneNumber" Margin="50,0,5,5" MinWidth="250"/> 
    <ComboBox Grid.Column="1" Grid.Row="0" Height="{Binding ElementName=phoneNumber, Path=Height}" MinWidth="100" Name="callResultsSelection" ItemsSource="{Binding Source={StaticResource callResults}}" Margin="0,0,5,5"/> 
</StackPanel> 

我需要能夠然後使用一個按鈕的按下導出這些的.Text & .SelectedItem值。我嘗試使用下面的屬性,但它似乎不工作。它確實通過控件的IntelliSense公開.Text屬性,但它不會按照預期將任何內容複製到剪貼板。

原始(和所需的)的方法:

public string Text 
    { 
     get { return phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n"; } 
     set { value = phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n"; } 
    } 

回退的方法:

public string Text 
    { 
     get { return phoneNumber.Text; } 
     set { value = phoneNumber.Text; } 
    }   

    public string ComboBoxSelection 
    { 
     get { return callResultsSelection.SelectedItem.ToString(); } 
     set { value = callResultsSelection.SelectedItem.ToString(); } 
    } 

我使用的控制迭代如下。這些部分還有很多,但這是唯一相關的部分。

foreach (object o in ccChildren.GetChildren(tool, 3)) 
      { 
       if (o.GetType() == typeof(CallTemplate)) 
       { 
        CallTemplate template = (CallTemplate)o; 
        if (template.Text != null) 
        { 
         textBuffer += template.Text; 
        } 
        else 
        { 
         textBuffer = ""; 
        } 
        tempString += textBuffer; 
        textBuffer = ""; 
       } 
      } 

通過使用斷點,我知道,它確實達到在if塊的決策點,但即使VS識別CallTemplate對象,它不匹配。任何人都看到了問題?

編輯:我知道問題不在於迭代方法(ccChildren.GetChildren)。我使用這個與其他衆多控件(文本框,組合框,單選按鈕,複選框),它工作得很好。該領域唯一可能成爲問題的是CallTemplate類型。

+1

在屬性.Text @集{}你應該使用值,而不是分配它! – mrbm 2012-03-02 17:36:19

+0

else { textBuffer =「」; } 似乎是錯的? – mrbm 2012-03-02 17:41:16

+0

我把值切換到等號的另一邊,沒有效果。至於textBuffer =「」,迭代的控件是一系列標籤和文本框。在if塊之前,該標籤被添加到textBuffer中。但是,如果標籤引用的控件爲空,則標籤不需要添加到tempString中,因此沒有空行。 – 2012-03-02 17:49:04

回答

0

所以這將是純粹愚蠢的時刻之一。我唯一的問題是我沒有將包含UserControl的GroupBox添加到GroupBox的數組中以迭代。感謝有用的回覆,雖然...

0

這是用戶控制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:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <StackPanel> 
     <TextBox Grid.Column="0" Grid.Row="0" Name="phoneNumber" Margin="50,0,5,5" MinWidth="250"/> 
     <ComboBox Grid.Column="1" Grid.Row="0" Height="{Binding ElementName=phoneNumber, Path=Height}" MinWidth="100" Name="callResultsSelection" ItemsSource="{Binding stud}" DisplayMemberPath="Name" Margin="0,0,5,5"/> 
    </StackPanel> 

</Grid> 

這是用戶控件

using System.Windows.Controls; 
using System.Collections.ObjectModel; 
namespace WpfApplication1 
{ 
    public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      stud = new ObservableCollection<Student>(); 
      stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" }); 
      stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" }); 
      stud.Add(new Student() { Name = "chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" }); 
      stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" }); 
      stud.Add(new Student() { Name = "chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" }); 
     } 
     public ObservableCollection<Student> stud 
     { get; set; } 
     public string Text 
     { 
      get 
      { 
       return phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n"; 
      } 
     } 
    } 
    public class Student 
    { 
     public string Name { get; set; } 
     public int RollNo { get; set; } 
     public string About { get; set; } 
    } 
} 

的代碼隱藏這是窗口XAML其中上述用戶控件用於

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

    xmlns:uc="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 

    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
     <uc:UserControl1 Grid.Row="0" x:Name="ucw"/> 
     <Button Click="Button_Click" Grid.Row="1"/> 
    </Grid> 
</Window> 

及以下一世s代碼在窗口後面

using System.Windows; 
namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    {  
     public MainWindow() 
     { 
      InitializeComponent(); 

      DataContext = this; 

     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show(ucw.Text); 
     } 
    } 
} 

這裏,當我點擊按鈕時,usercontrol的Text屬性在消息框中給出了正確的值。希望這會有所幫助。