2014-01-15 18 views
2

我有db填充我的列表框,並在下一步之前,我想有一個機會來定製用戶選擇tooglebuttons。嘗試獲取代碼後面的按鈕,它嵌套在數據模板

所以我想要了解他在代碼隱藏方面的選擇信息。我在這answer找到了使用VisualTreeHelper的代碼。

這是我的XAML代碼

<!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <controls:Pivot> 
      <controls:PivotItem> 
       <ListBox x:Name="PizzaList" SelectionChanged="PizzaList_SelectionChanged" IsSynchronizedWithCurrentItem="false" > 
        <ListBox.ItemTemplate> 
         <DataTemplate x:Name="template"> 

          <toolkit:ExpanderView Header="{Binding Nazwa}" x:Name="expander" Style="{StaticResource ExpanderViewStyle}"> 
           <toolkit:ExpanderView.Items> 
            <!--first stack panel would contain all elements which would be showed 
            after clicking on listbox item--> 
            <StackPanel Margin="20,0,0,0" Orientation="Vertical"> 
             <TextBlock HorizontalAlignment="Left" Text="Rozmiar"></TextBlock>        
             <!-- this stack panel contains 2 buttons which are 
             connected to sizes of pizza --> 
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
              <ToggleButton Width="200"> 
               <!--this how would look the button's content--> 
               <StackPanel HorizontalAlignment="Center" Orientation="Vertical"> 
                <TextBlock>&#8960;28cm</TextBlock> 
                <TextBlock Text="{Binding Cenaa}"></TextBlock> 
               </StackPanel> 
              </ToggleButton> 
              <ToggleButton Width="200"> 
               <StackPanel HorizontalAlignment="Center" Orientation="Vertical"> 
                <TextBlock>&#8960;50cm</TextBlock> 
                <TextBlock Text="{Binding Cenab}"></TextBlock> 
               </StackPanel> 
              </ToggleButton> 
             </StackPanel> 
             <!-- here part of code which would show type of cake option--> 
             <TextBlock Margin="0,12,0,0">Grubość ciasta:</TextBlock> 
             <StackPanel Orientation="Horizontal"> 
              <ToggleButton x:Name="small" IsChecked="true"> 
               <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">small</TextBlock> 
              </ToggleButton> 
              <ToggleButton x:Name="middle" Checked="Grubosc_Checked"> 
               <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">middle</TextBlock> 
              </ToggleButton> 
              <ToggleButton x:Name="fat" Checked="Grubosc_Checked"> 
               <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">fat</TextBlock> 
              </ToggleButton> 
             </StackPanel> 
             <Button>edit</Button> 
             <Button>basket</Button> 
            </StackPanel> 
           </toolkit:ExpanderView.Items> 
           <toolkit:ExpanderView.Expander> 
            <TextBlock Text="{Binding Skladniki}" Width="500"></TextBlock> 
           </toolkit:ExpanderView.Expander> 
          </toolkit:ExpanderView> 


         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </controls:PivotItem> 
     </controls:Pivot> 
    </Grid> 

,這裏是從答案所採取的方法:

 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; 
     } 

,我試圖通過這條線找到我的孩子

var found = FindChild<ToggleButton>(template, "small"); 

和那麼我得到這一行中的錯誤:

int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 

「System.InvalidOperationException」類型的異常出現在System.Windows.ni.dll但在用戶代碼中沒有處理」

什麼是我的錯?我應該如何訪問這個按鈕?

回答

0

那麼,在你的FindChild方法的某個地方有一個錯誤,很難說,沒有調試。 嘗試此方法(我寫了3年時間,卻沒有任何問題):

public static T FindVisualChildByName<T> (this FrameworkElement elem, string name) where T : DependencyObject 
    { 
     for (var i = 0; i < VisualTreeHelper.GetChildrenCount (elem); i++) 
     { 
      var child = VisualTreeHelper.GetChild (elem, i) as FrameworkElement; 
      if (child == null) 
       continue; 

      var controlName = child.GetValue (Control.NameProperty) as string; 
      if (controlName == name) 
       return child as T; 

      var result = FindVisualChildByName<T> (child, name); 
      if (result != null) 
       return result; 
     } 
     return null; 
    } 
+0

THX,我的切換按鈕被發現:) – MyWay