每当细胞获得焦点时,我都需要扩展器扩展。
除此之外,我希望扩展器在细胞失去焦点时收缩。
<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"
答案 0 :(得分:2)
“这个解决方案只是扩展而不是缩减。”
单击你的扩展器将其扩展一次,然后尝试单击datagridcell中的其他位置,它将无法工作,第一次手动扩展时绑定会被破坏,当datagridcell IsFocused为true时,扩展器不会自动扩展了。如果在viewmodel中对一个简单的bool属性使用单向绑定,也会发生同样的情况。
编辑:
尝试使用这个扩展器,我相信它可能只是你需要的东西:
的.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;
}
}
答案 1 :(得分:0)
这是一个具有较少依赖关系的解决方案(如果您尚未使用混合行为):
<Expander Header="Hello" Content="Goodbye">
<Expander.Style>
<Style TargetType="Expander">
<Style.Triggers>
<EventTrigger RoutedEvent="LostFocus">
<EventTrigger.Actions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard TargetProperty="IsExpanded">
<BooleanAnimationUsingKeyFrames>
<DiscreteBooleanKeyFrame Value="False" KeyTime="0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="GotFocus">
<EventTrigger.Actions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard TargetProperty="IsExpanded">
<BooleanAnimationUsingKeyFrames>
<DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
</Expander>