Wpf 使用DataGridComboxColumn作为DataGrid中的自动完成ComboxBox
我想使用DataGridComboBoxColumn作为自动完成组合框 我已经让它部分工作了。当行处于编辑模式时,我可以在组合框中键入文本,同样在视图模式下,控件返回文本。仅如何通过鼠标双击将标签(模板中)设置为编辑模式 首先,我不想使用DataGridTemplateColumn控件,因为它不像DataGridComboxColumn那样处理键盘和鼠标输入(选项卡、箭头、编辑/查看模式/双击等) 它看起来像:Wpf 使用DataGridComboxColumn作为DataGrid中的自动完成ComboxBox,wpf,mvvm,Wpf,Mvvm,我想使用DataGridComboBoxColumn作为自动完成组合框 我已经让它部分工作了。当行处于编辑模式时,我可以在组合框中键入文本,同样在视图模式下,控件返回文本。仅如何通过鼠标双击将标签(模板中)设置为编辑模式 首先,我不想使用DataGridTemplateColumn控件,因为它不像DataGridComboxColumn那样处理键盘和鼠标输入(选项卡、箭头、编辑/查看模式/双击等) 它看起来像: 我修复了它,在文本框中添加了一个行为,以获取到父数据网格的链接,然后通过调用Begi
我修复了它,在
文本框中添加了一个行为,以获取到父数据网格的链接,然后通过调用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"); }
}
}
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" });
}
}
公共类模型:BaseModel
{
//组合框的对象列表
私有列表(objectList),;
公共列表对象列表{get{return}OU ObjectList;}set{{OU ObjectList=value;}}
//数据网格中的行
私人物品清单;
公开物品清单
{
获取{return\u things;}
设置{u things=value;OnPropertyChanged(“things”)}
}
}
公共类事物:基本模型
{
//组合框中的文本
私有字符串_objectText;
公共字符串ObjectText
{
获取{return\u objectText;}
设置{u objectText=value;OnPropertyChanged(“objectText”);}
}
}
视图模型
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"); }
}
}
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" });
}
}
公共类视图模型
{
公共模型模型{get;set;}
公共视图模型()
{
Model=新的WpfApplication1.Model();
Model.ObjectList=新列表();
Model.ObjectList.Add(“AAAA”);
Model.ObjectList.Add(“bbb”);
Model.ObjectList.Add(“Ccccc”);
Model.Things=newlist();
添加(newthing(){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();
}
}
公共类CellSelectedBehavior
{
公共静态bool GetIsCellRowSelected(dependencyobjectobj){return(bool)obj.GetValue(IsCellRowSelectedProperty);}
公共静态void SetIsCellRowSelected(DependencyObject对象,布尔值){obj.SetValue(IsCellRowSelectedProperty,值);}
公共静态只读DependencyProperty IsCellRowSelectedProperty=DependencyProperty.RegisterAttached(“IsCellRowSelected”,
typeof(bool)、typeof(CellSelectedBehavior)、新UIPropertyMetadata(false,OnIsCellRowSelected));
静态无效OnIsCellRowSelected(DependencyObject depObj、DependencyPropertyChangedEventArgs e)
{
TextBox item=depObj作为TextBox;
如果(项==null)
返回;
如果(e.NewValue为bool==false)
返回;
if((bool)e.NewValue)
item.MouseDoubleClick+=SelectRow;
其他的
item.mousedoupleclick-=选择行;
}
静态void SelectRow(对象发送方、事件参数e)
{
TextBox=发送者作为TextBox;
var grid=box.FindAncestor();
grid.BeginEdit();
}
}
助手(查找数据网格)
公共静态类帮助器
{
公共静态T FindAncestor(此DependencyObject当前),其中T:DependencyObject
{
当前=VisualTreeHelper.GetParent(当前);
while(当前!=null)
{
如果(电流为T)
{
返回(T)电流;
}
当前=VisualTreeHelper.GetParent(当前);
};
返回null;
}
}