I want to create a new Pivot Item through C# during runtime, displaying a list Bx of the type ListBoxWithCheckBoxes from the toolkit, facilitating to toggle checkboxes visible or invisible in the left side.
My current version works, as far as drawing the new pivot Page, and binding items to it. But i can't get the ListBoxWithCheckBoxes to work properly.
This is from my cs file:
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
Additional info: I suspect it to have something to do with namespaces. on the XAML, this is added:
xmlns:my="clr-namespace:System.Windows.Controls;assembly=WindowsPhoneListBoxWithCheckBoxesControl"
And a normal ListBoxWithCheckBoxes, which is not made via c# in runtime, works fine. this is made this way:
<my:ListBoxWithCheckBoxes x:Name="FancyListBox" Margin="0,0,-12,0" HorizontalAlignment="Stretch" ItemsSource="{Binding Items}" >
Register for Loaded event on MyPivotItem and set "IsInChooseState" to "true" in the event handler
private void MyPivotItem_Loaded(object sender, RoutedEventArgs e)
{
MyPivotItem pivotItem = sender as MyPivotItem;
pivotItem.myListBox.IsInChooseState = true;
}