XAML 如何访问ContentView中的所有控件

uurity8g  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(175)

我在Maui项目中遇到XAML问题。免责声明:我不是这方面的Maven,所以请你饶了我吧;).
这是我的xaml:

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ApeEvoPrototype.NumPad">
    <ScrollView>
        <Grid 
            ColumnDefinitions="{DynamicResource NumPad_GridColumnPrincipale}" 
            RowDefinitions="{DynamicResource NumPad_GridRowPrincipale}">
            <Frame
                Grid.Row="0"
                Grid.Column="0"
                Style="{DynamicResource NumPad_Frame}">
                <Grid x:Name="numPadGrid" Style="{DynamicResource NumPad_GridInterno}">
                    
                    <StackLayout Grid.Row="0" Grid.ColumnSpan="3">
                        <Grid x:Name="containerEditor" Style="{DynamicResource NumPad_GridLinguetta}">
                            
                            <Label Grid.Column="0" VerticalTextAlignment="Center" Text="Quantity:" TextColor="Black" />

                            <Editor Grid.Column="1" x:Name="txtQuantity" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>

                            <Button Grid.Column="2" x:Name="btnBck" Text="Bck" Clicked="Button_Clicked_Bck" BindingContext="2000" />
                        </Grid>

                    </StackLayout>
                    <StackLayout Grid.Column="0" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn1" Style="{DynamicResource NumPad_Button}" Text="1" Clicked="Button_Clicked" BindingContext="1"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn4" Style="{DynamicResource NumPad_Button}" Text="4" Clicked="Button_Clicked" BindingContext="4"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn7" Style="{DynamicResource NumPad_Button}" Text="7" Clicked="Button_Clicked" BindingContext="7"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btnTot" Style="{DynamicResource NumPad_Button}" Text="All" Clicked="Button_Clicked_Tot" BindingContext="3000" />
                        </Grid>
                    </StackLayout>
                    
                    <StackLayout Grid.Column="1" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn2" Style="{DynamicResource NumPad_Button}" Text="2" Clicked="Button_Clicked" BindingContext="2"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn5" Style="{DynamicResource NumPad_Button}" Text="5" Clicked="Button_Clicked" BindingContext="5"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn8" Style="{DynamicResource NumPad_Button}" Text="8" Clicked="Button_Clicked" BindingContext="8"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn0" Style="{DynamicResource NumPad_Button}" Text="0" Clicked="Button_Clicked" BindingContext="0"/>
                        </Grid>
                    </StackLayout>

                    <StackLayout Grid.Column="2" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn3" Style="{DynamicResource NumPad_Button}" Text="3" Clicked="Button_Clicked" BindingContext="3"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn6" Style="{DynamicResource NumPad_Button}" Text="6" Clicked="Button_Clicked" BindingContext="6" />
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btn9" Style="{DynamicResource NumPad_Button}" Text="9" Clicked="Button_Clicked" BindingContext="9"/>
                        </Grid>
                        <Grid Style="{DynamicResource NumPad_GridTastierino}">
                            <Button x:Name="btnC" Style="{DynamicResource NumPad_Button}" Text="C" Clicked="Button_Clicked_C" BindingContext="1000" />
                        </Grid>
                    </StackLayout>

                </Grid>
            </Frame>

        </Grid>
    </ScrollView>
</ContentView>

正如您所看到的,我有很多名为“btn?"的按钮,我的目标是动态访问这些按钮,而不使用它们的名称。唯一的方法是定义包含按钮的单个网格的名称,就像我在这里所做的那样:

<Grid x:Name="containerEditor" Style="{DynamicResource NumPad_GridLinguetta}">

并在我代码中执行:

var buttons = containerEditor.Children.OfType<Button>();

这样我就可以访问Grid中的所有资源按钮。这种方法的问题是Children方法只探索控件的第一层,它不能递归地工作。
有人能帮帮我吗?

xwbd5t1u

xwbd5t1u1#

像这样的方法应该可以。

public static IEnumerable<T> FindChildrenOfType<T>(DependencyObject parent) where T : DependencyObject
{
    if (parent is ContentControl contentControl)
    {
        if (contentControl.Content is T contentOfT)
        {
            yield return contentOfT;
        }

        if (contentControl.Content is DependencyObject dependencyObjectContent)
        {
            foreach (T grandChild in FindChildrenOfType<T>(dependencyObjectContent))
            {
                yield return grandChild;
            }
        }
    }
    else
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);

            if (child is T childOfT)
            {
                yield return childOfT;
            }

            foreach (T grandChild in FindChildrenOfType<T>(child))
            {
                yield return grandChild;
            }
        }
    }
}

将您的根命名为“Root”(在此示例中),并按如下方式使用:

var buttons = FindChildrenOfType<Button>(this.Root)
    .Where(x => x.Name.StartsWith("btn"))
    .ToList();
xeufq47z

xeufq47z2#

我通过将所有控件处理到视图中来解决List问题

List<IVisualTreeElement> listButton = (List<IVisualTreeElement>)numPadGrid.GetVisualTreeDescendants();
foreach (IVisualTreeElement button in listButton)
{
    if (button is Button)
    {
        Button buttonCatch = (Button)button;
        ButtonCatch.BorderColor = ColorBorderButton;
    }
 }

相关问题