C# WPF - 如何以编程方式将组合框移动到数据网格单元格上
C# WPF - how to move a comboxbox over a datagrid cell programmatically
我将 WPF 与 C# 一起使用
我有一个 DataGrid,我需要在其中显示一个组合框。
我知道我可以通过向 DataGrid 添加一个 DataGridComboBoxColumn 来做到这一点,但是我对该列中的每个单元格都有组合框。
但我需要做的是只为列中的某些单元格显示一个组合框。
所以我尝试的是通过使用 Cell.PointToScreen() 和 TranslateTransform() 将组合框移动到单击单元格的位置,在当前数据网格单元格上显示组合框(cboSelectToleranz):
这是我尝试过的方法(代码隐藏)但它不起作用(组合框从屏幕上消失...)
private void myDatagrid_onCurrentCellChanged(object sender, EventArgs e)
{
if (!displayCombobox) return;
//get screen postion of cell that was clicked
//
var cellContent = myDatagrid.CurrentCell.Column.GetCellContent(myDatagrid.CurrentCell.Item);
DataGridCell cell = cellContent.Parent as DataGridCell;
Point Position = cell.PointToScreen(new Point(0, 0));
//try to move a combobox over current cell
//
var tt = new TranslateTransform();
tt.X = Position.X;
tt.Y = Position.Y;
cboSelectToleranz.RenderTransform = tt;
}
您可以尝试使用 DataGridTemplateColumn。
在数据模板中,您可以定义 ComboBox 并将其可见性绑定到模型的 属性。
您可以使用 DataGridTemplateColumn along with DataTemplateSelector。定义您的模板,其中之一是 ComboBox
.
DataTemplateSelector
将根据绑定的值选择合适的模板 属性:
// example of custom type and simple DataTemplateSelector
public class YourItemType
{
public string Property { get; set; }
}
public class YourTemplateSelector : DataTemplateSelector
{
public DataTemplate TextTemplate { get; set; }
public DataTemplate ComboTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return (item is YourItemType yourItem && yourItem.Property.Equals("condition here")) ? ComboTemplate : TextTemplate;
}
}
<!--Somewhere in static resources-->
<DataTemplate x:Key="comboTemplate">
<ComboBox IsEditable='False'
ItemsSource='{Binding YourItemsSource}' SelectedItem='{Binding Property}'
Text='{Binding Property}' />
</DataTemplate>
<DataTemplate x:Key="textTemplate">
<ComboBox Text='{Binding Property}' />
</DataTemplate>
<local:YourTemplateSelector x:Key='YourTemplateSelector'
TextTemplate='{StaticResource textTemplate}'
ComboTemplate='{StaticResource comboTemplate}' />
我将 WPF 与 C# 一起使用
我有一个 DataGrid,我需要在其中显示一个组合框。 我知道我可以通过向 DataGrid 添加一个 DataGridComboBoxColumn 来做到这一点,但是我对该列中的每个单元格都有组合框。
但我需要做的是只为列中的某些单元格显示一个组合框。 所以我尝试的是通过使用 Cell.PointToScreen() 和 TranslateTransform() 将组合框移动到单击单元格的位置,在当前数据网格单元格上显示组合框(cboSelectToleranz):
这是我尝试过的方法(代码隐藏)但它不起作用(组合框从屏幕上消失...)
private void myDatagrid_onCurrentCellChanged(object sender, EventArgs e)
{
if (!displayCombobox) return;
//get screen postion of cell that was clicked
//
var cellContent = myDatagrid.CurrentCell.Column.GetCellContent(myDatagrid.CurrentCell.Item);
DataGridCell cell = cellContent.Parent as DataGridCell;
Point Position = cell.PointToScreen(new Point(0, 0));
//try to move a combobox over current cell
//
var tt = new TranslateTransform();
tt.X = Position.X;
tt.Y = Position.Y;
cboSelectToleranz.RenderTransform = tt;
}
您可以尝试使用 DataGridTemplateColumn。
在数据模板中,您可以定义 ComboBox 并将其可见性绑定到模型的 属性。
您可以使用 DataGridTemplateColumn along with DataTemplateSelector。定义您的模板,其中之一是 ComboBox
.
DataTemplateSelector
将根据绑定的值选择合适的模板 属性:
// example of custom type and simple DataTemplateSelector
public class YourItemType
{
public string Property { get; set; }
}
public class YourTemplateSelector : DataTemplateSelector
{
public DataTemplate TextTemplate { get; set; }
public DataTemplate ComboTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return (item is YourItemType yourItem && yourItem.Property.Equals("condition here")) ? ComboTemplate : TextTemplate;
}
}
<!--Somewhere in static resources-->
<DataTemplate x:Key="comboTemplate">
<ComboBox IsEditable='False'
ItemsSource='{Binding YourItemsSource}' SelectedItem='{Binding Property}'
Text='{Binding Property}' />
</DataTemplate>
<DataTemplate x:Key="textTemplate">
<ComboBox Text='{Binding Property}' />
</DataTemplate>
<local:YourTemplateSelector x:Key='YourTemplateSelector'
TextTemplate='{StaticResource textTemplate}'
ComboTemplate='{StaticResource comboTemplate}' />