2
내 목록 상자를 채우는 DB를 가지고 있으며 다음 단계 전에 tooglebuttons를 사용하여 사용자를 선택할 수있는 기회를 갖기를 원합니다.데이터 템플릿에 중첩 된 코드 숨김 버튼에 넣으려고 시도합니다.
그래서 codebehind에서 그의 선택에 대한 정보를 얻고 싶습니다. 나는이 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>⌀28cm</TextBlock>
<TextBlock Text="{Binding Cenaa}"></TextBlock>
</StackPanel>
</ToggleButton>
<ToggleButton Width="200">
<StackPanel HorizontalAlignment="Center" Orientation="Vertical">
<TextBlock>⌀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에서 발생했지만 사용자 코드에서 처리되지 않았다 "
내 실수는 무엇입니까? 이 버튼에 어떻게 접근해야합니까?
들으, 내 토글 버튼이 발견되었다 : – MyWay