【发布时间】: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 如果您将其添加为答案,我会接受它。