wpf - 合约扩展器失去焦点

标签 wpf xaml datagrid styles expander

每当单元格获得焦点时,我需要扩展器来扩展。

除此之外,我希望每当单元失去焦点时扩展器就会收缩。

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

相关文章:

javascript - 内联编辑面临困难

c# - 将文本列排序为数字列WPF MVVM

c# - PnP WPF/Silverlight 复合应用程序指南是否适合基于角色的 UI 组合?

wpf - 如何在数据模板中实例化 ViewModel

c# - 如何为 ListBoxItem 创建标题?

c# - 绑定(bind)数据网格列可见性 MVVM

c# - 如何将此 View 绑定(bind)到此 ViewModel?

c# - WPF 中的显示对话框

html - 在 WP7 中显示 HTML

.net - 如何加速 .NET DataGrid 到 Excel 的导出?