首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >DataGridComboBoxColumn -单击即可自动下拉

DataGridComboBoxColumn -单击即可自动下拉
EN

Stack Overflow用户
提问于 2011-11-08 05:19:47
回答 4查看 11K关注 0票数 12

我在DataGrid中有一个DataGridComboBoxColum。我希望能够在单元格上单击一次,然后让组合框下拉。目前,我必须点击多次。

代码语言:javascript
代码运行次数:0
运行
复制
  <DataGrid AutoGenerateColumns="False" Height="148" HorizontalAlignment="Left" Margin="48,85,0,0" Name ="dg_display" VerticalAlignment="Top" Width="645"  CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding}" SelectionChanged="DgDisplaySelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Header="Symbol" Binding="{Binding Symbol}" />
            <DataGridTextColumn IsReadOnly="True" Header="Company ID" Binding="{Binding CompanyID}" />
            <DataGridComboBoxColumn IsReadOnly="False" Header="Sector" SelectedValueBinding="{Binding Sector}" DisplayMemberPath="{Binding [0]}" Visibility="Visible" >
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding SectorList}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding SectorList}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-01 06:35:36

一键式DataGridComboBoxColumn编辑+一键式CheckboxColumn编辑

另请参阅:https://stackoverflow.com/a/8333704/724944

XAML:

代码语言:javascript
代码运行次数:0
运行
复制
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
            <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />
        </Style>

代码隐藏:

代码语言:javascript
代码运行次数:0
运行
复制
    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        GridColumnFastEdit(cell, e);
    }

    private void DataGridCell_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        GridColumnFastEdit(cell, e);
    }

    private static void GridColumnFastEdit(DataGridCell cell, RoutedEventArgs e)
    {
        if (cell == null || cell.IsEditing || cell.IsReadOnly)
            return;

        DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
        if (dataGrid == null)
            return;

        if (!cell.IsFocused)
        {
            cell.Focus();
        }

        if (cell.Content is CheckBox)
        {
            if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
            {
                if (!cell.IsSelected)
                    cell.IsSelected = true;
            }
            else
            {
                DataGridRow row = FindVisualParent<DataGridRow>(cell);
                if (row != null && !row.IsSelected)
                {
                    row.IsSelected = true;
                }
            }
        }
        else
        {
            ComboBox cb = cell.Content as ComboBox;
            if (cb != null)
            {
                //DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                dataGrid.BeginEdit(e);
                cell.Dispatcher.Invoke(
                 DispatcherPriority.Background,
                 new Action(delegate { }));
                cb.IsDropDownOpen = true;
            }
        }
    }


    private static T FindVisualParent<T>(UIElement element) where T : UIElement
    {
        UIElement parent = element;
        while (parent != null)
        {
            T correctlyTyped = parent as T;
            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }

            parent = VisualTreeHelper.GetParent(parent) as UIElement;
        }
        return null;
    }
票数 10
EN

Stack Overflow用户

发布于 2019-01-17 21:19:38

其他两个答案对我都不起作用。不过,对我来说起作用的是以下几点。

代码语言:javascript
代码运行次数:0
运行
复制
<DataGridComboBoxColumn
    Header="Example ComboBox"
    DisplayMemberPath="Name"
    SelectedValuePath="Id">
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

我不记得我是从哪里得到这个解决方案的。它可能来自堆栈溢出。如果有人看到了原创来源,请随时发表评论。

票数 6
EN

Stack Overflow用户

发布于 2016-06-24 13:01:47

我对@surfen的回答有问题,可能是因为很多年后,WPF可能已经发生了很大的变化。看起来DataGrid现在为你做了一些事情,比如当你开始输入时自动编辑一个文本字段。

我对我的组合框列使用DataGridTemplateColumn。该模板的CellTemplate有一个TextBlock。调用BeginEdit,然后调用dispatcher,会导致组合框出现在可视化树中。然后,看起来鼠标单击被发送到组合框,并且它会自动打开。

以下是我对@surfen代码的修改版本:

代码语言:javascript
代码运行次数:0
运行
复制
public static class DataGridExtensions
{
    public static void FastEdit(this DataGrid dataGrid)
    {
        dataGrid.ThrowIfNull(nameof(dataGrid));

        dataGrid.PreviewMouseLeftButtonDown += (sender, args) => { FastEdit(args.OriginalSource, args); };
    }

    private static void FastEdit(object source, RoutedEventArgs args)
    {
        var dataGridCell = (source as UIElement)?.FindVisualParent<DataGridCell>();

        if (dataGridCell == null || dataGridCell.IsEditing || dataGridCell.IsReadOnly)
        {
            return;
        }

        var dataGrid = dataGridCell.FindVisualParent<DataGrid>();

        if (dataGrid == null)
        {
            return;
        }

        if (!dataGridCell.IsFocused)
        {
            dataGridCell.Focus();
        }

        if (dataGridCell.Content is CheckBox)
        {
            if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
            {
                if (!dataGridCell.IsSelected)
                {
                    dataGridCell.IsSelected = true;
                }
            }
            else
            {
                var dataGridRow = dataGridCell.FindVisualParent<DataGridRow>();

                if (dataGridRow != null && !dataGridRow.IsSelected)
                {
                    dataGridRow.IsSelected = true;
                }
            }
        }
        else
        {
            dataGrid.BeginEdit(args);

            dataGridCell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { }));
        }
    }
}

public static class UIElementExtensions
{
    public static T FindVisualParent<T>(this UIElement element)
        where T : UIElement
    {
        UIElement currentElement = element;

        while (currentElement != null)
        {
            var correctlyTyped = currentElement as T;

            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }

            currentElement = VisualTreeHelper.GetParent(currentElement) as UIElement;
        }

        return null;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8042743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档