2017-01-25 5 views
1

何らかの理由で、動的メソッドを使用してUIを生成する必要があります。以下は私のコードのスニペットです。WPFのUIエレメント(内部エキスパンダー)を名前で取得する

 string xaml = ""; 
     UIElement elementM = null; 
     xaml = xaml + "<StackPanel Orientation='Vertical'>"; 

     //Init & FreeSyn 
     xaml = xaml + String.Format("<Expander Margin='{0} {1} 0 0' VerticalAlignment='Top'>", iLeftMostPadding, iTopPadding); 
     xaml = xaml + "<Expander.Style>"; 
     xaml = xaml + "<Style TargetType='Expander'>"; 
     xaml = xaml + "<Setter Property='IsExpanded' Value='True' />"; 
     xaml = xaml + "<Setter Property='Header' Value='Axes'/>"; 
     xaml = xaml + "</Style>"; 
     xaml = xaml + "</Expander.Style>"; 
     xaml = xaml + "<Border BorderBrush='DodgerBlue' BorderThickness='1' Margin='3 3 3 0'>"; 

     xaml = xaml + "<StackPanel Orientation='Vertical'>"; 
     xaml = xaml + "<StackPanel Orientation='Horizontal'>"; 
     xaml = xaml + String.Format("<Button Margin='{0} {1} 0 5' Name='btnCheckAll' Background='{2}' Foreground='{3}' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='{4}'>Check All</Button>", iLeftPadding, iTopPadding, szBtnBG, szBtnFG, 82); 
     xaml = xaml + String.Format("<Button Margin='{0} {1} 0 5' Name='btnUncheckAll' Background='{2}' Foreground='{3}' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='{4}'>Uncheck All</Button>", iLeftPadding, iTopPadding, szBtnBG, szBtnFG, 82); 
     xaml = xaml + "</StackPanel>"; 

     int nRow = nAxisCount/4; 
     if (nAxisCount % 4 != 0) { nRow++; } 

     xaml = xaml + String.Format(" <UniformGrid Columns='4' Rows='{0}'>", nRow); 
     for (int i = 0; i < nAxisCount; i++) 
     { 
      xaml = xaml + String.Format("<CheckBox Height='20' Name='chkIsAxis{0}' Margin='{1} {2} 0 0' Width='40' IsChecked = 'true'>{0}</CheckBox>", i, 0, iTopPadding); 
     } 

     xaml = xaml + "</UniformGrid>"; 
     xaml = xaml + "</StackPanel>"; 
     xaml = xaml + "</Border>"; 
     xaml = xaml + "</Expander>"; 

elementM = (UIElement)XamlReader.Parse(xaml, context); 
gridM.Children.Add(elementM); 

string szTempM = ""; 

szTempM = "btnCheckAll"; 
Button btnCheckAll = CHelper.FindChild<Button>(elementM, szTempM); //Return Null Here !!! 
btnCheckAll.Click += btnCheckAll_Click; 

私は名前で&「のチェックを外しすべて」を「すべてをチェック」ボタンを取得するために失敗します。 両方のボタンをエクスパンダの外側に割り当てると成功します。 ボタンを名前で取得する方法は知っていますか? (エキスパンダー内)

これにonClickイベントを追加する必要があります。

ありがとうございます。

このようなUIの外観:以下

enter image description here

は私FindChildコードです:あなたはXAMLマークアップで定義ボタン要素まで

public static T FindChild<T>(DependencyObject parent, string childName) 
     where T : DependencyObject 
    { 
     // Confirm parent and childName are valid. 
     if (parent == null) return null; 

     T foundChild = null; 

     int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < childrenCount; i++) 
     { 
      var child = VisualTreeHelper.GetChild(parent, i); 

      // If the child is not of the request child type child 
      T childType = child as T; 
      if (childType == null) 
      { 
       // recursively drill down the tree 
       foundChild = FindChild<T>(child, childName); 

       // If the child is found, break so we do not overwrite the found child. 
       if (foundChild != null) break; 
      } 
      else if (!string.IsNullOrEmpty(childName)) 
      { 
       var frameworkElement = child as FrameworkElement; 
       // If the child's name is set for search 
       if (frameworkElement != null && frameworkElement.Name == childName) 
       { 
        // if the child's name is of the request name 

        foundChild = (T)child; 
        break; 
       } 
      } 
      else 
      { 
       // child element found. 
       foundChild = (T)child; 
       break; 
      } 
     } 

     return foundChild; 
    } 

答えて

1

待ちは実際にあなたの前に作成されていますそれらへの参照を取得しようとします。あなたはLoadedイベントを処理できます:

... 
elementM = (UIElement)XamlReader.Parse(xaml); 
gridM.Children.Add(elementM); 

FrameworkElement fe = elementM as FrameworkElement; 
if (fe != null) 
{ 
    fe.Loaded += (s, e) => 
    { 
     string szTempM = "btnCheckAll"; 
     Button btnCheckAll = CHelper.FindChild<Button>(fe, szTempM); 
     btnCheckAll.Click += btnCheckAll_Click; 
    }; 
} 
+0

ありがとう! (expanderを設定すると動作します) –

関連する問題