- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 DataGridComboBoxColumn 作为自动完成组合框。
我已经部分工作了。当行处于编辑模式时,我可以在组合框中键入文本,也在 View 模式下控件返回文本。只是如何通过鼠标双击将标签(模板中)设置为EditMode?
首先,我不想使用 DataGridTemplateColumn 控件,因为它不能像 DataGridComboBoxColumn 那样处理键盘和鼠标输入(选项卡、箭头、编辑/查看模式/双击等...)。
看起来像:
最佳答案
我修复了这个问题,向 TextBox
添加了一个行为,以获取指向父 DataGrid 的链接,然后通过调用 BeginEdit()
将行设置为编辑模式。
我使用的解决方案:
查看
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Model.Things}" Name="MyGrid" ClipboardCopyMode="IncludeHeader">
<DataGrid.Resources>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Object" MinWidth="140" TextBinding="{Binding ObjectText}" ItemsSource="{Binding Source={StaticResource proxy}, Path=Data.Model.ObjectList}" >
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True"/>
<Setter Property="Text" Value="{Binding ObjectText}"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox IsReadOnly="True" Text="{Binding Path=DataContext.ObjectText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}">
<TextBox.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="local:CellSelectedBehavior.IsCellRowSelected" Value="true"></Setter>
</Style>
</TextBox.Resources>
</TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
型号
public class Model : BaseModel
{
//List of objects for combobox
private List<string> _objectList;
public List<string> ObjectList { get { return _objectList; } set { _objectList = value; } }
//Rows in datagrid
private List<Thing> _things;
public List<Thing> Things
{
get { return _things; }
set { _things = value; OnPropertyChanged("Things"); }
}
}
public class Thing : BaseModel
{
//Text in combobox
private string _objectText;
public string ObjectText
{
get { return _objectText; }
set { _objectText = value; OnPropertyChanged("ObjectText"); }
}
}
View 模型
public class ViewModel
{
public Model Model { get; set; }
public ViewModel()
{
Model = new WpfApplication1.Model();
Model.ObjectList = new List<string>();
Model.ObjectList.Add("Aaaaa");
Model.ObjectList.Add("Bbbbb");
Model.ObjectList.Add("Ccccc");
Model.Things = new List<Thing>();
Model.Things.Add(new Thing() { ObjectText = "Aaaaa" });
}
}
行为
public class CellSelectedBehavior
{
public static bool GetIsCellRowSelected(DependencyObject obj) { return (bool)obj.GetValue(IsCellRowSelectedProperty); }
public static void SetIsCellRowSelected(DependencyObject obj, bool value) { obj.SetValue(IsCellRowSelectedProperty, value); }
public static readonly DependencyProperty IsCellRowSelectedProperty = DependencyProperty.RegisterAttached("IsCellRowSelected",
typeof(bool), typeof(CellSelectedBehavior), new UIPropertyMetadata(false, OnIsCellRowSelected));
static void OnIsCellRowSelected(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
TextBox item = depObj as TextBox;
if (item == null)
return;
if (e.NewValue is bool == false)
return;
if ((bool)e.NewValue)
item.MouseDoubleClick += SelectRow;
else
item.MouseDoubleClick -= SelectRow;
}
static void SelectRow(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
var grid = box.FindAncestor<DataGrid>();
grid.BeginEdit();
}
}
助手(查找 DataGrid)
public static class Helper
{
public static T FindAncestor<T>(this DependencyObject current) where T : DependencyObject
{
current = VisualTreeHelper.GetParent(current);
while (current != null)
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}
}
关于wpf - 在 DataGrid 中使用 DataGridComboBoxColumn 作为自动完成组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14559902/
您好,我正在使用 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 的
我是一名优秀的程序员,十分优秀!