- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 DataGrid 中有一个 DataGridComboBoxColum。我希望能够单击单元格一次并下拉组合框。目前我必须点击多次。
<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>
最佳答案
一键DataGridComboBoxColumn编辑+一键CheckboxColumn编辑
另见: https://stackoverflow.com/a/8333704/724944
XAML:
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
<EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />
</Style>
代码隐藏:
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;
}
关于c# - DataGridComboBoxColumn - 单击时自动下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8042743/
您好,我正在使用 WPF DataGrid,并且正在尝试使 ComboBox Column 正常工作。 当我查看网
我正在 WPF 中用 C# 3.5 编写一个应用程序。我想以编程方式将 C# 中的 DataGridComboBoxColumn 添加到 WPF 工具包 DataGrid。问题是控件本身似乎不存在,因
我有一个包含 2 列的数据网格。一列包含角色信息,另一列应该有一个包含可用用户列表的组合框。组合框中的数据与第一列中的数据无关。 组合框没有数据上下文,只有一个项目源,而且我似乎也无法使用绑定(bin
我想知道如何让 DataGridComboBoxColumn 在不处于编辑模式时显示所选项目的文本?是否可以使编辑模式下的组合框最初显示所选项目的文本? 这是我的 XAML:
加载数据网格时是否可以“暴露”WPF 工具包数据网格的 DataGridComboBoxColumn?默认情况下,您必须单击单元格才能显示组合框。我希望用户看到组合框可用,而无需单击单元格。我希望组合
我在 DataGrid 中有一个 DataGridComboBoxColum。我希望能够单击单元格一次并下拉组合框。目前我必须点击多次。
我有一个带有 DatagridComboBoxColumn 的数据网格,我想在用户从 ComboBox 中选择任何东西时触发事件 SelectionChanged,执行一些操作,我该怎么做任何建议,谢
我有一个带有 DataGridComboBoxColumn 的网格,我正在尝试更改单元格的前景色(不处于编辑状态)。 我知道我可以使用 DataGridTemplateColumn 解决这个问题,但我
我在 WPF 和 DataGridComboBoxColumn 中使用 Datagrid。请在下面找到代码: ...
当我单击 DataGridComboBoxColumn 中的单元格时,组合框变得可见,我可以选择项目。当我选择了一个项目时,它在顶部可见。但是,当单元格又名 ComboBox 因为我单击 DataGr
我正在尝试数据绑定(bind) DataGridComboBoxColumn 我在这里做错了什么,因
我正在使用 C#、.NET Framework 4.5.1、MVVM Light 框架和 Entity Framework 6.1.3 开发 WPF。 我有这个DataGrid在一个窗口中:
我有一个 ViewModel(其结构的伪代码): class ViewModel { public List Packages { get; set; } } enum Type {
我有一个 DataGrid,里面有几个 ComboBox 列。使用 ViewModel 中的 IDataErrorInfo 接口(interface)验证这些值。将鼠标悬停在适当的单元格上时,工具提示
我正在尝试将 T 的 ObservableCollection 绑定(bind)到 DataGrid 的 DataGridComboBoxColumn。 DataGrid 定义是:
我目前正在努力使用 C# WPF 中的 DataGridComboBoxColumn。 我有 ToolModel 类 class ToolModel { public long Id { ge
我在 WPF 项目的 DataGrid 中有一个 DataGridComboBoxColumn 设置如下: 但是当我运行该项目时,该列仅显示空白值,并且编辑模式下的组合框执行相同的操作。 DataG
示例代码: class GameListViewModel { private IGameRepository repository; public GameViewModel Gam
我有一个数据网格,在 DataGrid.AutoGeneratingColumn 事件中,我将一些列更改为 DataGridComboBoxColumns。 与此同时,我还想向该新组合框添加一个选择更
我是 WPF 的新手,我遇到了以下情况: class Person{ string Name; List ListAddresses; } 我有一个带有 ItemsSource 的
我是一名优秀的程序员,十分优秀!