使用ListBoxWithCheckBoxes在运行时创建数据透视表项

时间:2012-12-06 03:28:51

标签: c# xaml windows-phone-7.1 pivot listbox-control

我想在运行时通过C#创建一个新的Pivot Item,从工具包中显示ListBoxWithCheckBoxes类型的列表Bx,便于在左侧切换可见或不可见的复选框。

我的当前版本可以工作,就绘制新的枢轴页面和绑定项目而言。但是我无法使ListBoxWithCheckBox工作正常。

这是来自我的cs文件:

var itemTemplate = 
                @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">  
                    <StackPanel Margin=""0,0,0,17"" HorizontalAlignment=""Stretch"" Height=""78"" Orientation=""Vertical""> 
                        <TextBlock Text=""{Binding Title}"" TextWrapping=""Wrap"" Style=""{StaticResource PhoneTextExtraLargeStyle}"" Width=""Auto""/>
                        <TextBlock Text=""{Binding Description}"" TextWrapping=""Wrap"" Margin=""12,-6,12,0"" Style=""{StaticResource PhoneTextSubtleStyle}"" Width=""Auto""/>
                    </StackPanel>  
                 </DataTemplate>";


            //Creating Pivot Item
            PivotItem newPiv = new PivotItem(); 
            newPiv.Header = "Pivot Header"; //defining a header

            //Content for the Pivot Item
            ListBoxWithCheckBoxes newList = new ListBoxWithCheckBoxes(); //new listbox
            newList.ItemsSource = App.ViewModel.Items; //Grapping some items
            newList.ItemTemplate = (DataTemplate)XamlReader.Load(itemTemplate); //using xaml template

            //Adding the list to the Pivot Item
            newPiv.Content = newList; //Adding list to Pivot Item
            MainItemList.Items.Add(newPiv); //Adding Pivot Item

其他信息: 我怀疑它与名称空间有关。在XAML上,添加了:

xmlns:my="clr-namespace:System.Windows.Controls;assembly=WindowsPhoneListBoxWithCheckBoxesControl"

一个正常的ListBoxWithCheckBoxes,它不是在运行时通过c#生成的,工作正常。这是这样做的:

<my:ListBoxWithCheckBoxes x:Name="FancyListBox" Margin="0,0,-12,0" HorizontalAlignment="Stretch" ItemsSource="{Binding Items}" >

1 个答案:

答案 0 :(得分:1)

在MyPivotItem上注册Loaded事件,并在事件处理程序中将“IsInChooseState”设置为“true”

private void MyPivotItem_Loaded(object sender, RoutedEventArgs e)
{
    MyPivotItem pivotItem = sender as MyPivotItem;
    pivotItem.myListBox.IsInChooseState = true;
}