Reputation: 1268
OK...
This is confusing me...I have a way I can think of doing this...but I wouldn't know where to get started.
I would like to add a PivotItem in code for example...
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);
}
Now, where I am running into my problem is setting the content of the PivotItem.
I would like to set this code as the content:
<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>
What I can think of doing IS: Somehow creating another .XAML sheet with the above code in it, however, how would I declare:
pvItem.Content = XAMLSheet.xaml;
Unless there is another way, I think that is how I'll have to do it.
I also need the events to work and the tags should be applied using the code instead of databinding like I had it originally.
I tried doing this, but, it only shows it as text not code...
pvItem.Content = "<TextBlock Text=\"Hello\" />";
Thank you.
Upvotes: 0
Views: 2871
Reputation: 574
Here's a solution that will allow you to assign the content of a PivotItem
dynamically: Create a UserControl with your content declared in the layout root:
<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>
Then in your code where you are creating the PivotItem
, create a new instance of your user control, making sure to set the DataContext
and ItemsSource
properties to support your bindings:
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);
}
Upvotes: 1
Reputation: 50672
An alternative would be to NOT use code at all but instead use databinding to fill the Pivot.
Databinding PivotItems to ObservableCollection on WP7
Upvotes: 0
Reputation: 1268
I figured mine out somewhat...
I can't think of another way than this:
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;
Is there anyway to improve the code / make it less sloppy?
Thanks.
Upvotes: 0
Reputation: 31724
If you really have to add it the code from a xaml snippet you could do XamlReader.Load(), though I am not sure why you would need that. If you want to have a Pivot built from a collection - why not just bind to its ItemsSource and put your xaml as the ItemTemplate? You would probably need to get rid of the event handlers and instead change the IsChecked bindings to Mode=TwoWay.
Upvotes: 0