gpt4 book ai didi

c# - 尝试获取嵌套在数据模板中的按钮后面的代码

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:25 24 4
gpt4 key购买 nike

我有数据库填充我的列表框,在下一步之前,我希望有机会使用 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;
}

我试着通过这条线找到我的 child

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

然后我在这一行中得到了错误:

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

An exception of type 'System.InvalidOperationException' occurred in System.Windows.ni.dll but was not handled in user code"

我的错误是什么?我应该如何访问此按钮?

最佳答案

好吧,在您的 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;
}

关于c# - 尝试获取嵌套在数据模板中的按钮后面的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com