使用 Silverlight 手风琴控件进行数据绑定

发布于 2024-08-18 23:01:10 字数 1338 浏览 3 评论 0原文

我在 ChildWindow 中有 Silverlight Accordion 控件,并按以下方式对其进行了自定义

<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>

现在我的 Chilwindow.Xaml 中有一个方法

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

该方法是从主页调用的,它传递 gp 值

Groups 是一个具有公共属性和 Observable 集合的类。现在

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;} 
   --------
   -------
}

我必须将 _headertext(标头模板中的 TextBlock)与 FirstName 绑定,将 _contenttext(内容模板中的 TextBlock)与 LastName 绑定。

请帮我做这件事。我需要你的帮助。

谢谢 拉尼

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 in 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-08-25 23:01:10

为什么不在 XAML 中直接使用数据绑定?您不需要在代码中执行此操作。

<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> 

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> 
蓝天白云 2024-08-25 23:01:10

首先,TargetType 指向 AccordionItem,并且您尝试在 Accordion 元素本身上使用该样式。这永远不会起作用。为了使其正常工作,您需要创建两种样式,一种用于手风琴本身,另一种用于您在手风琴样式中引用的 AccordionItem。

    <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>

然后你像这样定义你的手风琴控件:

    <layoutToolkit:Accordion Height="Auto"
                             Name="accordion1" 
                             ExpandDirection="Right" 
                             SelectionMode="One"
                             ItemsSource="{Binding}"
                             Style="{StaticResource accordionStyle}">
    </layoutToolkit: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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文