1
我想在運行時通過C#創建一個新的數據透視表項,從工具箱中顯示ListBoxWithCheckBoxes類型的列表Bx,便於在左側切換可見或不可見的複選框。使用ListBoxWithCheckBoxes在運行時創建數據透視表項
我的當前版本在繪製新的樞軸頁面和綁定項目時起作用。但我無法使ListBoxWithCheckBoxes正常工作。
這是從我的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}" >