【问题标题】:Binding DataGrid Expander when using ListCollectionView使用 ListCollectionView 时绑定 DataGrid Expander
【发布时间】:2019-10-03 19:06:44
【问题描述】:

我正在尝试使用如下示例所示的打开/关闭按钮来打开和关闭所有 DataGrid 扩展器。问题是当我使用单个布尔值绑定到 this 时,当我打开/关闭单个扩展器时会中断。

我正在使用 ListCollectionView 来设置 GroupingDescription。

我想我正在寻找一种解决方案,以某种方式让 Expander 发挥出色?

查看

<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander IsExpanded="{Binding Source={StaticResource proxy}, Path=Data.Expanded, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                                <Expander.Header>
                                    <StackPanel>
                                        <!-- label -->
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

视图模型

public bool Expanded
{
    get { return _expanded; }
    set { _expanded = value; OnPropertyChanged(); }
}

public ListCollectionView Items
{
    get
    {
        return _items;
    }
    set
    {
        _items = value; OnPropertyChanged();
    }
}

// logic
var items = new ListCollectionView(planninglist);
items.SortDescriptions.Add(new items.GroupDescriptions.Add(new PropertyGroupDescription("AanZet"));
items.IsLiveSorting = true;

更新

根据 @XAMlMAX 在 cmets 中建议的 SO answer 进行修复。

public class ExpanderBehavior
{
    public static readonly DependencyProperty IsExpandedProperty =
  DependencyProperty.RegisterAttached("IsExpanded",
                                      typeof(bool),
                                      typeof(ExpanderBehavior),
                                      new PropertyMetadata(OnChanged));

    public static bool GetIsExpanded(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsExpandedProperty);
    }
    public static void SetIsExpanded(DependencyObject obj, bool value)
    {
        obj.SetValue(IsExpandedProperty, value);
    }

    private static void OnChanged(DependencyObject o,
                                  DependencyPropertyChangedEventArgs args)
    {
        Expander tb = o as Expander;
        if (null != tb)
            tb.IsExpanded = (bool)args.NewValue;
    }
}

【问题讨论】:

  • 输出窗口中是否出现任何绑定错误?你是用OneWay绑定只开一个群吗?
  • 这只是一个例子来展示我想要做什么。将所有扩展器绑定在一个属性上是行不通的。如果我使用两种方式绑定,那么当我只想打开/关闭一个时,所有扩展器都会打开/关闭。
  • 我很久以前就遇到过类似的问题,我求助于代码隐藏和遍历可视化树。我猜这不是你想做的事?
  • This 看起来很有希望。
  • @XAMlMAX 如果您将其添加为答案,我会接受它。

标签: wpf datagrid


【解决方案1】:

根据评论对话。
即使扩展器已绑定,您仍然遇到问题的原因是,当您使用 OneWay Binding 然后单击按钮时,它会断开连接。要更好地了解何时断开连接,请使用PresentationTraceSources.TraceLevel=High

要克服这个问题,可以使用附加属性来阻止绑定分离。
现在这个例子是针对ToggleButton,但逻辑应该是一样的。

public class TBExtender
{
    public static readonly DependencyProperty IsCheckedProperty =
       DependencyProperty.RegisterAttached("IsChecked", 
                                      typeof(bool),
                                      typeof(TBExtender),
                                      new PropertyMetadata(OnChanged));

    public static bool GetIsChecked(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsCheckedProperty);
    }
    public static void SetIsChecked(DependencyObject obj, bool value)
    {
      obj.SetValue(IsCheckedProperty, value);
    }

    private static void OnChanged(DependencyObject o,
                              DependencyPropertyChangedEventArgs args)
    {
        ToggleButton tb = o as ToggleButton;
        if (null != tb)
            tb.IsChecked = (bool)args.NewValue;
    }
}  

归功于alex-p
wallstreet-programmer
回复this SO question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2020-02-07
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多