2009-07-01 7 views
0

ドロップダウンなどの動的コントロールを生成するリストボックスを作成しました& datepicker。私は行内のデータを取得したかった。通常、Windowsフォームでは、Items [i] .FindControl( "ControlID")メソッドを一般的にインデックスします。 XAMLでどうやってやりますか?Silverlight:リストボックス内にネストされたコントロールを取得する

ボタンをクリックすると変更を取得する必要があります。

ところで、ここに私のXAMLの単純なビューがあります:

<ListBox> 
    <stackpanel> 
      <TextBlock /> 
      <stackpanel> 
       <grid> 
        <combobox /> 
        <combobox/> 
        <datepicker /> 
       </grid> 
      </stackpanel> 
    </stackpanel> 
</ListBox> 

はありがとうございました!

答えて

1

最も簡単な方法は、オブジェクトへのコントロールの双方向バインドを設定することです。オブジェクトは、値が設定された内容を示します。

また、リーフオブジェクトに到達するまで、オブジェクトのコンテンツプロパティを調べて、ツリーを通過することもできます。

また、選択項目を使用して、リーフオブジェクトになるまでVisualTreeHelper's GetChildメソッドを呼び出すこともできます。

2
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     FrameworkElement selectedItem = (sender as ListBox).SelectedItem as FrameworkElement; 
     List<FrameworkElement> children = new List<FrameworkElement>(); 
     children = GetChildren(selectedItem, ref children); 
    } 

    private List<FrameworkElement> GetChildren(FrameworkElement element, ref List<FrameworkElement> list) 
    { 
     int count = VisualTreeHelper.GetChildrenCount(element); 

     for (int i = 0; i < count; i++) 
     { 
      FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement; 

      if(child != null) 
      { 
       list.Add(child); 
       GetChildren(child, ref list); 
      } 
     } 

     return list; 
    } 

これは、すべてのFrameworkElements(パス、罫線などを含む)を返します。子を特定の型(ComboBox、StackPanelなど)である場合にのみ、簡単にその子を拡張してGetChildrenメソッドを再帰的に呼び出すことができます。

2

私はこの種のタスクを支援する次の2つのメソッドを備えています。

XAML:リストボックスを使用して

<ListBox Height="236" HorizontalAlignment="Left" Margin="31,23,0,0" 
      Name="listBox1" VerticalAlignment="Top" Width="245"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" Name="sp"> 
        <TextBlock Name="id">id</TextBlock> 
        <TextBox Name="test" Text="{Binding Key}"></TextBox> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

、あなたが選択した項目に渡すことができます。

var v1 =(ListBoxItem) listBox1.ItemContainerGenerator.ContainerFromIndex(
         listBox1.SelectedIndex); 


TextBox tb = GetChildByName<TextBox>(v1, "test"); 
tb.Text = "changed";  

し、その選択したリストボックス項目の正しいテキストボックスになるだろう。その参照を使用して、その参照をプロパティに変更できます。

public T GetChildByName<T>(DependencyObject parent, string name) 
      where T : class 
    { 
     T obj = RecGetChildByName<T>(parent, name) as T; 
     if (obj == null) throw new Exception("could find control " 
           + "of name as child"); 
     return obj; 
    } 

private DependencyObject RecGetChildByName<T>(DependencyObject parent, 
      string name) 
    { 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
     { 
      var child = VisualTreeHelper.GetChild(parent, i); 

      Control childControl = child as Control; 
      if (childControl != null) 
      { 
       if (childControl.Name == name) return child; 
      } 

      if (VisualTreeHelper.GetChildrenCount(child) > 0) 
       return RecGetChildByName<T>(child, name); 
     } 

     return null; 
    } 
関連する問題