Trupti
Trupti

Reputation: 1

How dynamically create PanoramaItem control in c#?

**<controls:PanoramaItem Header="first item">
            <!--Double line list with text wrapping-->
            <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="150">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock x:Name="Name" Text="Name: " TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="5,0,0,0"/>
                            </StackPanel>
                            <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            <TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            <TextBlock Text="{Binding LineFour}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>



            </ListBox>
        </controls:PanoramaItem>**

This is .xmal Part. but I have to do this in c#.Then Please Help me how i am do this.

Upvotes: 0

Views: 723

Answers (1)

Terkel
Terkel

Reputation: 1575

ListBox listBox = new ListBox();
listBox.Margin = new Thickness(0, 0, -12, 0);
listBox.SetBinding(ListBox.ItemsSourceProperty, new Binding("Items"))

CreateItemTemplate(listBox);

PanoramaItem pi = new PanoramaItem();
pi.Header = "first item";
pi.Content = listBox;

When implementing CreateItemTemplate you have two choices, either create the DataTemplate programmatically or create it in a ResourceDictionary as a resource and use that resource. The latter is by far the easiest and best way to do it.

To do it programatically see How to define a DataTemplate in code?

To use a resource you can something like this

public void CreateItemTemplate(ListBox listBox)
{
    object myDataTemplate = FindResource("myDataTemplateResource"); // This only works if the resource is available in the scope of your control. E.g. is defined in MyControl.Resources
    listBox.SetResourceReference(ListBox.ItemTemplateProperty, myDataTemplate);
}

Upvotes: 4

Related Questions