2012-03-02 8 views
1

ComboBoxからなるかなり単純なUserControlを作成しました。カスタムコントロール内の複数のコントロールのテキストと.Contentにアクセス

<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 @ set {}は値を使用する必要があります。 – mrbm

+0

else { textBuffer = ""; } が間違っているようですか? – mrbm

+0

私は等号の反対側に値を入れました。効果はありません。 textBuffer = ""の場合、反復処理を行うコントロールは一連のラベルとテキストボックスです。ifブロックの前に、ラベルがtextBufferに追加されます。しかし、ラベルが参照するコントロールが空の場合、空白行がないように、ラベルをtempStringに追加する必要はありません。 –

答えて

0

これは、まったくばかげた愚かさの瞬間のひとつです。私の唯一の問題は、GroupBoxの配列にUserControlを含むGroupBoxを追加していないということでした。しかし、有益な返信をお寄せいただきありがとうございます。

0

これは、このこれは、ユーザーコントロール上記ウィンドウ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> 

、以下で使用されているユーザーコントロール

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

<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; 
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プロパティは、メッセージボックスに正しい値を与えています。これが役立つことを願っています。

関連する問題