在枢轴内的列表框

时间:2015-10-08 21:38:55

标签: c# wpf xaml listbox uwp

我有这个代码。我需要从我的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);

获取exec作为listCont = null。我在做什么错了?

2 个答案:

答案 0 :(得分:2)

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

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

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

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

以下是我的样本,请尝试参考:

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

结果: enter image description here

答案 1 :(得分:0)

在故事板中声明Pivot并使用x:Key而不是x:Name。

e.g。

def A(): blabla # this will give an error def B(): print x # even though x is not defined, this does not give an error A() # same as above, NameError is only detected during runtime