如何将Expander标头绑定到扩展器内容中的第一项 [英] How do I bind Expander header to first item in expander content

查看:76
本文介绍了如何将Expander标头绑定到扩展器内容中的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个扩展器,其中包含一个列表作为内容,我试图将扩展器头绑定到列表中的第一项。



这里是simplfied xaml代码,后面没有代码,如果可能的话我想保持简洁:



Hello,

I have an Expander which contains a list as content and I am trying to bind the expander header to the first item in the list.

Here is the simplfied xaml code and there is no code behind, if possible I would like to keep it that way for simplicity:

<UserControl x:Class="OptiScanGui.AlarmPanel" 
      x:Name="AlarmPanelControl">

  <UserControl.Resources>
    <ResourceDictionary>
			
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="..\Resources\OptiStyles.xaml" />
        <ResourceDictionary Source="AlarmPanelStyles.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <CollectionViewSource x:Key="AlarmListCVS" 
            Source="{Binding  AlarmsList}">
                  
        <CollectionViewSource.SortDescriptions>
          <scm:SortDescription PropertyName="Priority" />
        </CollectionViewSource.SortDescriptions>
        
      </CollectionViewSource>

      <CollectionViewSource x:Key="AlarmHeaderCVS" 
            Source="{Binding Source={StaticResource AlarmListCVS}, 
            Path=[0]}" />
			
      <DataTemplate x:Key="AlarmItemTemplate" 
            DataType="{x:Type local:CurrentAlarm}">

        <Grid x:Name="AlarmItemLayout" >

          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>

          <StackPanel x:Name="AlarmItemStackPanel" >
          
            <TextBlock x:Name="AlarmTitleTextBlock" 
                  Text="{Binding Title}" />
                 
            <TextBlock x:Name="AlarmDetailsTextBlock" 
                  Text="{Binding Details}" />
                  
          </StackPanel>

          <Button x:Name="ResolveButton" Grid.Column="1"  
                Content="Resolve"/>
					
        </Grid>

      </DataTemplate>
			
    </ResourceDictionary>
		
  </UserControl.Resources>

  <Grid x:Name="LayoutRoot" >
		
    <Border x:Name="AlarmPanelBorder">
			
      <Grid x:Name="AlarmPanelLayout">

        <Expander x:Name="ListExpander" 
              DataContext="{Binding 
                    Source={StaticResource AlarmHeaderCVS}}" 
              HeaderTemplate="{StaticResource AlarmItemTemplate}"
              Style="{StaticResource AlarmsMainExpanderStyle}">

          <ListBox x:Name="AlarmsListBox"
                ItemsSource="{Binding 
                      Source={StaticResource AlarmListCVS}}"
                ItemTemplate="{StaticResource AlarmItemTemplate}"
                SelectedIndex="{Binding AlarmSelectedIndex}" />

        </Expander>
      </Grid>
    </Border>
  </Grid>
</UserControl>





我试图对Header和List Items使用相同的DataTemplate包含的功能,为简单起见,此处已删除。标题需要更改,因为更高优先级的项目将添加到按优先级排序的列表中。



所有反馈意见都赞赏,我在网上搜索并了解了很多,但我找不到任何工作。



谢谢



I am attempting to use the same DataTemplate for both the Header and List Items as both require the contained functionality, which was removed here for simplicity. The header needs to change as higher priority items are added to the list sorted on Priority.

All feedback appreciated, I have searched online and learned a lot but nothing I found works.

Thanks

推荐答案

编辑:如果列表更改,这将不会更新



仅供参考它的教育价值,你可以这样做:



this won't update if the list changes

Just for the educational value of it, you can do it like this:

<Expander Header="{Binding ElementName=t, Path=Items[0].Content}" IsExpanded="False">
    <ListBox x:Name="t">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>3</ListBoxItem>
        <ListBoxItem>4</ListBoxItem>
        <ListBoxItem>5</ListBoxItem>
    </ListBox>
</Expander>






但这很难看,当列表为空时会导致错误。这在纯Xaml中是不可能解决的。根据您填充列表的位置(AlarmsList),我建议在那里进行排序并创建一个属性,您可以绑定到Expander的Header并将其设置为您想要的项目(优先级最高的项目,首先列表中的项目。)




But that's ugly and can result in errors when the list is empty. This is impossible to solve in pure Xaml. Depending on where you fill the list you're binding to (AlarmsList), I would suggest to sort it there and create a property you can bind to the Header of the Expander and set that to the item you want (highest priority item, first item in list).


这篇关于如何将Expander标头绑定到扩展器内容中的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆