Datagrid 组合框消失的内容 + 自动完成
Datagrid combobox disappearing content + autocomplete
从昨天开始,我一直在尝试解决这个问题,但没有成功(谷歌搜索)。我在 DataGridTemplateColumn
中有一个组合框,当我 select 某物然后单击另一个单元格时,组合框的内容消失,但再次单击时仍然存在。向下滚动数据网格时,情况类似。
我已经尝试了几乎所有我在网上找到的东西,但没有任何成功。我不明白为什么会这样。
当我切换到 DataGridComboboxColumn
时,一切正常,但在这种情况下我无法实现可搜索(自动完成)组合框。
我在 xaml 中的组合框如下所示:
<DataGrid ItemsSource="{Binding ListOfCars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}"
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="150"
IsEditable="True"
IsDropDownOpen="False"
ItemsSource="{Binding Source={StaticResource TypeLists}}"
DisplayMemberPath="Type"
SelectedValuePath="IDType"
SelectedItem="{Binding SelectedType}"
SelectedValue="{Binding IDType,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="False">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
SelectedCar
是 Car
类型,ListOfCars
是 List<Car>
类型
SelectedValue
(IDType
) 是 Car
中的一个 属性 class.
我想实现两件事:
- 当 selected 时,即使我单击其他单元格,组合框中的项目也会保留。
- 有什么简单的方法可以实现自动完成组合框吗? (我尝试使用
DotNetKit.Wpf.AutoCompleteComboBox
,但在数据网格之外,它就像一个魅力,但内部情况与组合框相同......)
关于第一个问题,我复制了你的案例,做了一些修改。它对我来说很好用(Win10 VS pro 2019 .Net 4.7.2)。希望你能在那里找到答案。
我创造了你的类
public class Car
{
public int IDType { get; set; }
}
public class Type
{
public string Name { get; set; }
public int IDType { get; set; }
}
我用一些样本创建了一个视图模型
class ViewModel
{
public List<Car> ListOfCars { get; set; }
public Car SelectedCar { get; set; }
public List<Type> TypeLists { get; set; }
public ViewModel()
{
// Add five cars
ListOfCars = new List<Car>();
for (int i = 0; i < 5; i++)
{
ListOfCars.Add(new Car());
}
// Add three types: type0, type1, type2
TypeLists = new List<Type>();
for (int i = 0; i < 3; i++)
{
TypeLists.Add(new Type() { Name = $"type{i}", IDType = i });
}
}
}
对于View,我用的是main window
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
我在 Main Window XAML 中添加了数据网格,并进行了一些更改以配合我的视图模型。如果使用 UserControl,请在下面的代码中将 AncestorType 更改为 UserControl。
<DataGrid
ItemsSource="{Binding ListOfCars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="150"
IsEditable="True"
IsDropDownOpen="False"
ItemsSource="{Binding
Path = DataContext.TypeLists,
RelativeSource={RelativeSource FindAncestor,
AncestorType=Window}}"
DisplayMemberPath="Name"
SelectedValuePath="IDType"
SelectedValue="{Binding IDType,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="False">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我在下面 table,左栏是绑定到 TypeLists 的组合框,右栏是自动生成的 Car IDType。
我更改了一些组合框,然后滚动,一切就绪。
根据经验,数据从 window/usercontrol 中消失的原因有两个:绑定错误和向属性中注入空值。
当绑定错误时,数据不会被存储,任何 window 的刷新都会消失。在调试模式下检查 VS 的输出 window,如果有损坏的绑定,它会为您提供信息。
有时在导航页面时会将 null 注入到 属性 中。这可以通过在有问题的属性 setter 中添加以下行来避免:
if (value==null) return
从昨天开始,我一直在尝试解决这个问题,但没有成功(谷歌搜索)。我在 DataGridTemplateColumn
中有一个组合框,当我 select 某物然后单击另一个单元格时,组合框的内容消失,但再次单击时仍然存在。向下滚动数据网格时,情况类似。
我已经尝试了几乎所有我在网上找到的东西,但没有任何成功。我不明白为什么会这样。
当我切换到 DataGridComboboxColumn
时,一切正常,但在这种情况下我无法实现可搜索(自动完成)组合框。
我在 xaml 中的组合框如下所示:
<DataGrid ItemsSource="{Binding ListOfCars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}"
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="150"
IsEditable="True"
IsDropDownOpen="False"
ItemsSource="{Binding Source={StaticResource TypeLists}}"
DisplayMemberPath="Type"
SelectedValuePath="IDType"
SelectedItem="{Binding SelectedType}"
SelectedValue="{Binding IDType,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="False">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
SelectedCar
是 Car
类型,ListOfCars
是 List<Car>
类型
SelectedValue
(IDType
) 是 Car
中的一个 属性 class.
我想实现两件事:
- 当 selected 时,即使我单击其他单元格,组合框中的项目也会保留。
- 有什么简单的方法可以实现自动完成组合框吗? (我尝试使用
DotNetKit.Wpf.AutoCompleteComboBox
,但在数据网格之外,它就像一个魅力,但内部情况与组合框相同......)
关于第一个问题,我复制了你的案例,做了一些修改。它对我来说很好用(Win10 VS pro 2019 .Net 4.7.2)。希望你能在那里找到答案。
我创造了你的类
public class Car
{
public int IDType { get; set; }
}
public class Type
{
public string Name { get; set; }
public int IDType { get; set; }
}
我用一些样本创建了一个视图模型
class ViewModel
{
public List<Car> ListOfCars { get; set; }
public Car SelectedCar { get; set; }
public List<Type> TypeLists { get; set; }
public ViewModel()
{
// Add five cars
ListOfCars = new List<Car>();
for (int i = 0; i < 5; i++)
{
ListOfCars.Add(new Car());
}
// Add three types: type0, type1, type2
TypeLists = new List<Type>();
for (int i = 0; i < 3; i++)
{
TypeLists.Add(new Type() { Name = $"type{i}", IDType = i });
}
}
}
对于View,我用的是main window
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
我在 Main Window XAML 中添加了数据网格,并进行了一些更改以配合我的视图模型。如果使用 UserControl,请在下面的代码中将 AncestorType 更改为 UserControl。
<DataGrid
ItemsSource="{Binding ListOfCars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="150"
IsEditable="True"
IsDropDownOpen="False"
ItemsSource="{Binding
Path = DataContext.TypeLists,
RelativeSource={RelativeSource FindAncestor,
AncestorType=Window}}"
DisplayMemberPath="Name"
SelectedValuePath="IDType"
SelectedValue="{Binding IDType,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="False">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我在下面 table,左栏是绑定到 TypeLists 的组合框,右栏是自动生成的 Car IDType。
我更改了一些组合框,然后滚动,一切就绪。
根据经验,数据从 window/usercontrol 中消失的原因有两个:绑定错误和向属性中注入空值。
当绑定错误时,数据不会被存储,任何 window 的刷新都会消失。在调试模式下检查 VS 的输出 window,如果有损坏的绑定,它会为您提供信息。
有时在导航页面时会将 null 注入到 属性 中。这可以通过在有问题的属性 setter 中添加以下行来避免:
if (value==null) return