2011-11-10 20 views
0

好的......使用代碼的PivotItem內容?

這讓我感到困惑......我有辦法想到這樣做......但我不知道從哪裏開始。

我想在示例代碼中添加一個PivotItem ...

for (int i = 0; i < App.Accounts.Items.Count; i++) 
{ 
    PivotItem pvItem = new PivotItem(); 

    pvItem.Header = App.Accounts.Items[i].Username; 

    pvItem.Content = ""; 

    pivotControl.Items.Add(pvItem); 
} 

現在,在那裏我遇到了我的問題是設置PivotItem內容。

我想設置這個代碼內容:

  <ListBox x:Name="Accounts" Margin="0,0,-12,0"> 
       <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
        <StackPanel Width="455" x:Name="accWidth"> 
         <TextBlock Text="Personal Account" FontSize="32" Margin="0,0,0,0" TextAlignment="Center" /> 
         <toolkit:ToggleSwitch Header="Signed On" Tag="{Binding Tag}" IsChecked="{Binding SignedOn}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" /> 
         <toolkit:ToggleSwitch Header="Invisible Login" Tag="{Binding Tag}" IsChecked="{Binding InvisibleLogin}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" /> 
         <toolkit:ToggleSwitch Header="Show as Windows Phone 7" Tag="{Binding Tag}" IsChecked="{Binding MobileLogin}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" />        
        </StackPanel> 
       </StackPanel> 
      </ListBox> 

我能想到做的就是:不知怎的,創造在它上面的代碼中的另一個.XAML片,不過,我會怎樣聲明:

pvItem.Content = XAMLSheet.xaml; 

除非有另一種方式,我認爲這是我必須這樣做。

我還需要事件來工作和標籤應該使用代碼而不是像我原來的數據綁定應用。

我試着這樣做,但是,那隻能說明它作爲文本不代碼...

pvItem.Content = "<TextBlock Text=\"Hello\" />"; 

謝謝。

回答

0

我想通挖掘出幾分......

我想不出另一種方式莫過於:

  ListBox lb = new ListBox(); 

      lb.Margin = new Thickness(0, 0, -12, 0); 

      TextBlock tb = new TextBlock(); 

      tb.Text = App.Accounts.Items[i].Description; 

      tb.FontSize = 32; 

      tb.TextAlignment = TextAlignment.Center; 

      tb.Width = 455; 

      ToggleSwitch signOn = new ToggleSwitch(); 

      signOn.Tag = i.ToString(); 

      signOn.Header = "Signed On"; 

      signOn.IsChecked = App.Accounts.Items[i].SignedOn; 

      signOn.Checked += new EventHandler<RoutedEventArgs>(signOn_Checked); 

      signOn.Unchecked += new EventHandler<RoutedEventArgs>(signOn_Unchecked); 

      signOn.Width = 455; 

      ToggleSwitch invisLogin = new ToggleSwitch(); 

      invisLogin.Tag = i.ToString(); 

      invisLogin.Header = "Invisible Login"; 

      invisLogin.IsChecked = App.Accounts.Items[i].InvisibileLogin; 

      invisLogin.Checked += new EventHandler<RoutedEventArgs>(invisLogin_Checked); 

      invisLogin.Unchecked += new EventHandler<RoutedEventArgs>(invisLogin_Unchecked); 

      invisLogin.Width = 455; 

      ToggleSwitch wpLogin = new ToggleSwitch(); 

      wpLogin.Tag = i.ToString(); 

      wpLogin.Header = "Show as Windows Phone 7"; 

      wpLogin.IsChecked = App.Accounts.Items[i].InvisibileLogin; 

      wpLogin.Checked += new EventHandler<RoutedEventArgs>(wpLogin_Checked); 

      wpLogin.Unchecked += new EventHandler<RoutedEventArgs>(wpLogin_Unchecked); 

      wpLogin.Width = 455; 

      lb.Items.Add(tb); 

      lb.Items.Add(signOn); 

      lb.Items.Add(invisLogin); 

      lb.Items.Add(wpLogin); 

      pvItem.Content = lb; 

反正是有提高代碼/使其不那麼草率?

謝謝。

0

如果您確實需要從xaml片段中添加代碼,您可以執行XamlReader.Load(),但我不確定爲什麼您需要這樣做。 如果你想從一個集合構建一個Pivot - 爲什麼不只是綁定到它的ItemsSource並把你的xaml作爲ItemTemplate?您可能需要擺脫事件處理程序,而是將IsChecked綁定更改爲Mode = TwoWay。

1

這裏有一個解決方案,讓您的PivotItem的內容動態地分配:創建佈局根宣佈該內容的用戶控件:

<UserControl x:Class="MyUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="480" d:DesignWidth="480"> 

    <Grid x:Name="LayoutRoot"> 
     <ListBox x:Name="Accounts" Margin="0,0,-12,0"> 
      <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
       <StackPanel Width="455" x:Name="accWidth"> 
        <TextBlock Text="Personal Account" FontSize="32" Margin="0,0,0,0" TextAlignment="Center" /> 
        <toolkit:ToggleSwitch Header="Signed On" Tag="{Binding Tag}" IsChecked="{Binding SignedOn}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" /> 
        <toolkit:ToggleSwitch Header="Invisible Login" Tag="{Binding Tag}" IsChecked="{Binding InvisibleLogin}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" /> 
        <toolkit:ToggleSwitch Header="Show as Windows Phone 7" Tag="{Binding Tag}" IsChecked="{Binding MobileLogin}" Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" />        
       </StackPanel> 
      </StackPanel> 
     </ListBox> 
    </Grid> 
</UserControl> 
在你的代碼

然後在那裏你正在創建PivotItem,創建用戶控件的新實例,確保設置DataContextItemsSource屬性以支持您的綁定:

for (int i = 0; i < App.Accounts.Items.Count; i++) 
{ 
    PivotItem pvItem = new PivotItem(); 
    pvItem.Header = App.Accounts.Items[i].Username; 

    //instantiate your control and set the DataContext/ItemsSource to your model it will be bound to 
    MyUserControl myControl = new MyUserControl(); 
    myControl.DataContext = someModel; 
    myControl.List.ItemsSource = someModel.List; 

    //assign content to the instance of your user control 
    pvItem.Content = myControl;   

    pivotControl.Items.Add(pvItem); 
}