开发者

Data Binding with Silverlight accordion Control

I have Silverlight Accordion control in the ChildWindow and I customized it the following way

<Style x:Key=itemStyle TargetType=AccordionItem>
  <Setter Porperty=HeaderTemplate>
    <DataTemplate>
      <TextBlock x:Name=_headertext/>
    </DataTemplate>
  </Setter>
</Style>

<Accordion Style"{StaticResource itemStyle}">
  <Accordion.ContentTemplate>
    <DataTemplate>
      <StackPanel>
        <CheckBox/>
        <TextBlock x:name=_contenttext/>
    </DataTemplate>
  <Accordion.ContentTemplate>
</Accordion>

Now I have a method i开发者_JAVA技巧n my Chilwindow.Xaml

public void  LoadItems(ObservableColection<Groups> gp)
{}

This method is called from the mainpage and it passes the gp value

Groups is a class with public properties and Observable collections.For example

public class Groups
{
  public string FirstName{get, set;}

  public ObservableCollection<Details> details {get, set;}

  public Groups()
  {
    this.details=new ObservableCollection<Details>();
  }

}

My Details Class is as follows

public class Details
{
   public int id {get; set;}
   public string LastName{get; set;} 
   --------
   -------
}

Now I have to bind the _headertext(TextBlock in header Template) with the FirstName and _contenttext(TextBlock in Content Template) with LastName.

Please help me in doing this.I need your help.

Thanks Rani


Why not use databinding in XAML directly? You should not need to do this in code.

<Style x:Key=itemStyle TargetType=AccordionItem> 
  <Setter Porperty=HeaderTemplate> 
    <DataTemplate> 
      <TextBlock Text="{Binding FirstName}"/> 
    </DataTemplate> 
  </Setter> 
</Style> 

<Accordion Style"{StaticResource itemStyle}"> 
  <Accordion.ContentTemplate> 
    <DataTemplate> 
      <StackPanel> 
        <CheckBox/> 
        <TextBlock Text="{Binding LastName}"/> 
    </DataTemplate> 
  <Accordion.ContentTemplate> 
</Accordion> 


First, the TargetType is pointed at AccordionItem and you are trying to use the style on the Accordion element itself. This will never work. In order to get this to work, you will need to create two styles, one for the Accordion itself and one for the AccordionItem that you reference within the style for the accordion.

    <Style x:Key="itemStyle" TargetType="layoutToolkit:AccordionItem">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="accordionStyle" TargetType="layoutToolkit:Accordion">
        <Setter Property="ItemContainerStyle" Value="{StaticResource itemStyle}" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Content}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Then you define your accordion control like such:

    <layoutToolkit:Accordion Height="Auto"
                             Name="accordion1" 
                             ExpandDirection="Right" 
                             SelectionMode="One"
                             ItemsSource="{Binding}"
                             Style="{StaticResource accordionStyle}">
    </layoutToolkit:Accordion>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜