Question

I have a list of objects in a model. I wish to show elements of the DTO's in the list in my AccordianItem panels. The model is like this:

public class MyModel 
{ 
    public List<AnimalDTO> Items { get; set; } 

    public MyModel() 
    { 
        Items = new List<AnimalDTO> 
                    { 
                        new AnimalDTO() {Title = "Monkey", ImageUri = "Images/monkey.jpg"}, 
                        new AnimalDTO() {Title = "Cow", ImageUri = "Images/cow.jpg"}, 
                    }; 
    } 
} 
public class AnimalDTO
{
    public string Title { get; set; }
    public string LongDescription { get; set; }
    public string ImageUri { get; set; }
    public string NavigateUri { get; set; }
}

I want to show the image in the background image of AccordianItems and lay the LongDescription over a portion of the image.

If I hard code it, I can get the image in the AccordianItem thus...

<layoutToolkit:AccordionItem x:Name="Item2" Header="Item 2" Margin="0,0,10,0" AccordionButtonStyle="{StaticResource AccordionButtonStyle1}" ExpandableContentControlStyle="{StaticResource ExpandableContentControlStyle1}" HeaderTemplate="{StaticResource DataTemplate1}" BorderBrush="{x:Null}" ContentTemplate="{StaticResource CarouselContentTemplate}">
                <layoutToolkit:AccordionItem.Background>
                    <ImageBrush ImageSource="Images/cow.jpg" Stretch="None"/>
                </layoutToolkit:AccordionItem.Background>
            </layoutToolkit:AccordionItem>

When I try it with a binding like <ImageBrush ImageSource="{Binding Path={StaticResource MyContentTemplate.ImageUri}}" Stretch="None"/> or if I try it with <ImageBrush ImageSource="{Binding Path=Items[0].ImageUri}" Stretch="None"/> , it throws XamlParseException.

Edit: I'm able to get some binding of the text over hard-coded images with the following StaticResource (NOTE: I'm hard-coding Items[2], I'm not sure how to index it)

        <DataTemplate x:Key="CarouselContentTemplate">
        <Grid Width="650" Height="420">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.476*"/>
                <RowDefinition Height="0.524*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0"
                   x:Name="Title" 
                   Text="{Binding Items[2].Title}" 
                   Foreground="Black" FontSize="12"></TextBlock>
            <TextBlock Grid.Row="1" Grid.Column="0"
                   x:Name="LongDescription" 
                   Text="{Binding Items[2].LongDescription}"
                   TextWrapping="Wrap"FontSize="8"></TextBlock>
        </Grid>
    </DataTemplate>

Is there a way to index the Items collection in the DataTemplate? Furthermore how do I get the Image to bind rather than hard-coding them in each AccordianItem? Any help in the right direction would be appreciated, most especially how to bind and lay text over an image.

Was it helpful?

Solution

To bind to a collection it must be referenced with ItemsSource="{Binding Items}", where in this case Items is my collection MyModel.Items

<layoutToolkit:Accordion
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
        ExpandDirection="Right" 
        Style="{StaticResource AccordionStyle1}"
        AccordionButtonStyle="{StaticResource AccordionButtonStyle1}"
         MaxHeight="420" MaxWidth="800" 
         ItemsSource="{Binding Items}" Margin="8,0,-8,-12" Grid.Row="3"
        >
        <layoutToolkit:Accordion.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"></TextBlock>
            </DataTemplate>
        </layoutToolkit:Accordion.ItemTemplate>

Note that a collection should be bound with ItemsSource, which is plural as a mnemonic. and individual members of elements are bound within control of <layoutToolkit:Accordian.ItemTemplate> Here I am showing MyCollection.Title in a TextBlock control. I shall update this with full code or a link to my blog for a full example later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top