如果将项目添加到相应的组,则应扩展WPF ListBox GroupItem扩展器

时间:2016-12-19 13:21:09

标签: wpf listbox

我已经列出了一个列表框,其中的每个组项都包含在“扩展器”中。并且它一开始就崩溃了。将项目添加到列表框中的特定组时,应该扩展该组的相应扩展器(IsExpanded = true)。

以下是我迄今为止尝试过的风格。我在这里错过了什么吗?

 <form name="myForm">
   <label>
   Enter your name:
   <input type="text"
       name="myName"
       ng-model="name"
       ng-minlength="5"
       ng-maxlength="20"
       ng-pattern="^[^\w+$]"
       required />
  </label>
  <pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>

  <div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
    <div ng-message="required">You did not enter a field</div>
    <div ng-message="minlength">Your field is too short</div>
    <div ng-message="maxlength">Your field is too long</div>
 </div>
 </form>

1 个答案:

答案 0 :(得分:1)

您需要将Expander的IsExpanded属性设置为true才能展开它。例如,您可以通过订阅组中项目的CollectionChanged事件来执行此操作:

private void Expander_Loaded(object sender, RoutedEventArgs e)
{
    Expander expander = sender as Expander;
    CollectionViewGroup cvs = expander.DataContext as CollectionViewGroup;
    if (cvs != null)
    {
        INotifyCollectionChanged coll = cvs.Items as INotifyCollectionChanged;
        if (coll != null)
        {
            WeakEventManager<INotifyCollectionChanged, NotifyCollectionChangedEventArgs>.AddHandler(coll, "CollectionChanged",
                (ss, ee) => expander.IsExpanded = true);
        }
    }

}
<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Expander IsExpanded="False" Loaded="Expander_Loaded">
                    <Expander.Header>
                        <Border>
                            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/>
                        </Border>
                    </Expander.Header>
                    <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="Black" BorderThickness="0.5">
                        <ItemsPresenter Margin="5,0,0,5"/>
                    </Border>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

或者您可以将Expander的IsExpanded属性绑定到您的某些源属性。有关此内容的更多信息,请参阅以下问题。

wpf datagrid automatically expand first group