【问题标题】:Listbox inside pivot枢轴内的列表框
【发布时间】:2015-10-08 21:38:55
【问题描述】:

我有这个代码。我需要从我的 c# 代码中访问 ScheduleList。但它是无法访问的。我只能访问 SchedulePivot。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,50">
    <Pivot  x:Name="SchedulePivot" Margin="10,10,10,0" Title="Pivot" VerticalAlignment="Top">
        <Pivot.ItemTemplate>
            <DataTemplate>
                <ListBox x:Name="ScheduleList" Margin="0,0,0,17" Width="Auto">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="52" Width="auto">

在 StackOverflow 上搜索我发现了这段代码:

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
        {
            int childNumber = VisualTreeHelper.GetChildrenCount(control);
            for (int i = 0; i < childNumber; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(control, i);
                FrameworkElement fe = child as FrameworkElement;
                // Not a framework element or is null
                if (fe == null) return null;

                if (child is T && fe.Name == ctrlName)
                {
                    // Found the control so return
                    return child;
                }
                else
                {
                    // Not found it - search children
                    DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                    if (nextLevel != null)
                        return nextLevel;
                }
            }
            return null;
        }

我用这条线来获取孩子:

ListBox listCont = FindChildControl<ListBox>(this, "ScheduleList") as ListBox;

我也试过这样做:

ListBox listCont = FindChildControl<ListBox>(SchedulePivot, "ScheduleList") as ListBox;

比我这样做:

listCont.Items.Add(items);

并将例外设为 listCont=null。我怎么了?

【问题讨论】:

  • 您何时(以及从何处)调用 FindChildControl?
  • 由于 xaml 代码位于 MainPage.xaml 中,我在 MainPage.xaml.cs 中声明了此方法,并从 void 调用它,当单击按钮时会运行跨度>

标签: c# wpf xaml listbox uwp


【解决方案1】:

我已经测试了您的代码,以下两个代码在我身边都运行良好,我可以得到正确的结果:

ListBox listCont = FindChildControl<ListBox>(this, "ScheduleList") as ListBox;
ListBox listCont = FindChildControl<ListBox>(SchedulePivot, "ScheduleList") as ListBox;

如果我们想通过VisualTreeHelper访问控件,我们应该确保我们没有在MainPage的构造函数中调用上面的代码,否则我们会得到如下的空结果。因为控件没有完全初始化:

为了得到正确的结果,我们需要在MainPage.Loaded事件或按钮点击事件中调用上面的代码,以确保控件已经完全初始化,之后它应该可以正常工作了。

以下是我的示例,请尝试参考:

在 MainPage.xaml 中:

<Pivot x:Name="SchedulePivot" ItemsSource="{Binding PivotTestlist}" Margin="10,10,10,0" Title="Pivot" VerticalAlignment="Top">
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding header}"></TextBlock>
            </DataTemplate>
        </Pivot.HeaderTemplate>
        <Pivot.ItemTemplate>
            <DataTemplate>
                <ListBox x:Name="ScheduleList" Margin="0,0,0,17" Width="Auto" ItemsSource="{Binding ListBoxTestlist}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="52" Width="auto">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding id}"></TextBlock>
                                    <TextBlock Text="{Binding name}"></TextBlock>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DataTemplate>
        </Pivot.ItemTemplate>
    </Pivot>

    <Button Click="Button_Click" Content="Button"></Button>

在 MainPage.xaml.cs 中:

 public class ListBoxTest
{
    public string name { get; set; }
    public string id { get; set; }
}

public class PivotTest
{
    public List<ListBoxTest> ListBoxTestlist { get; set; }
    public string header { get; set; }
}
public sealed partial class MainPage : Page
{
    public List<PivotTest> PivotTestlist { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        PivotTestlist = new List<PivotTest>();
        PivotTest PivotTest1 = new PivotTest();
        PivotTest1.ListBoxTestlist = new List<ListBoxTest>();
        PivotTest1.ListBoxTestlist.Add(new ListBoxTest() { name = "name1", id = "id1" });
        PivotTest1.ListBoxTestlist.Add(new ListBoxTest() { name = "name2", id = "id2" });
        PivotTest1.header = "header1";
        PivotTestlist.Add(PivotTest1);
        PivotTest PivotTest2 = new PivotTest();
        PivotTest2.ListBoxTestlist = new List<ListBoxTest>();
        PivotTest2.ListBoxTestlist.Add(new ListBoxTest() { name = "name11", id = "id11" });
        PivotTest2.ListBoxTestlist.Add(new ListBoxTest() { name = "name22", id = "id22" });
        PivotTest2.header = "header2";
        PivotTestlist.Add(PivotTest2);
        this.DataContext = this;

    }
    private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ListBox listCont = FindChildControl<ListBox>(SchedulePivot, "ScheduleList") as ListBox;
        int count = listCont.Items.Count;
    }
}

结果:

【讨论】:

  • 我已经检查过了,它不起作用。也许我们可以通过 Skype 或电子邮件联系,这样您就可以帮助我
  • 能否将您的项目与 GitHub 或 Onedrive 中的 .cs 代码共享?这样我就可以测试你的项目看看哪里出错了。
【解决方案2】:

在情节提要中声明 Pivot 并使用 x:Key 而不是 x:Name。

例如

<StoryBoard> <Pivot x:key="nameIt"/> </StoryBoard> private void AccesPivot () { //now you can acces your pivot }

【讨论】:

  • 我需要访问列表框,而不是枢轴
  • 对不起,我没有仔细阅读。您可以做与枢轴基本相同的事情,在情节提要中声明列表框,为其分配一个 x:key 并在后面的代码中使用它
  • windows 通用应用不支持故事板
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多