每当单元格获得焦点时,我需要扩展器来扩展。
除此之外,我希望每当单元失去焦点时扩展器就会收缩。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander VerticalAlignment="Stretch"
IsExpanded="{Binding Path=IsFocused,
Mode=OneWay,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType=DataGridCell},
UpdateSourceTrigger=LostFocus}">
<!--Expander.Content-->
</Expander>
</DataTemplate>
</DataGirdTemplateColumn.CellTemplate>
这个解决方案只是扩展而不是收缩。
我哪里错了?
注释:
DataGrid.SelectionMode="Single"
DataGrid.SelectionUnit="Cell"
最佳答案
"This solution is only expanding but not contracting."
单击您的扩展器将其扩展一次,然后尝试单击 datagridcell 中的其他位置,它将不起作用,第一次手动扩展时绑定(bind)会被破坏,并且当 datagridcell IsFocused 为 true 时扩展器不会自动扩展不再了。如果您在 View 模型中的简单 bool 属性上使用单向绑定(bind),也会发生同样的情况。
编辑:
尝试使用这个扩展器,我相信它可能会满足您的需要:
.xaml
<local:MyExpander DataGridCellToObserve="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell}}" >
.cs
public class MyExpander : Expander
{
public MyExpander()
{
this.IsEnabled = false;
}
public DataGridCell DataGridCellToObserve
{
get { return (DataGridCell)GetValue(DataGridCellToObserveProperty); }
set { SetValue(DataGridCellToObserveProperty, value); }
}
public static readonly DependencyProperty DataGridCellToObserveProperty =
DependencyProperty.Register("DataGridCellToObserve", typeof(DataGridCell), typeof(MyExpander), new PropertyMetadata(test));
private static void test(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(e.NewValue as DataGridCell).Selected += (d as MyExpander).setExpanded;
(e.NewValue as DataGridCell).Unselected += (d as MyExpander).setExpandedFalse;
}
private void setExpanded(object sender, RoutedEventArgs e)
{
this.SetValue(IsExpandedProperty, true);
this.IsEnabled = true;
}
void setExpandedFalse(object sender, RoutedEventArgs e)
{
this.SetValue(IsExpandedProperty, false);
this.IsEnabled = false;
}
}
关于wpf - 合约扩展器失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48460045/