合同扩展器失去焦点

Contract expander on losing focus

我需要扩展器在单元格获得焦点时扩展。

除此之外,我希望扩展器在单元格失去焦点时收缩。

<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."

单击您的扩展器将其扩展一次,然后尝试单击您的数据网格单元格中的其他位置,它不会起作用,第一次手动扩展时绑定会被破坏,并且当数据网格单元格被聚焦时扩展器不会自动扩展是真的了。如果您在视图模型中对简单的 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;
    }
}

这是一个依赖性较少的解决方案(如果您还没有使用 Blend Behaviors):

<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>