如何将自定义对象列表绑定到 WPF 中的 DataGridComboboxColumn?
How to bind a list of a custom object to a DataGridComboboxColumn in WPF?
我在这个网站上找到了一些例子,但我找不到任何我可以用在我的例子中的东西。
我有一个带有 AutoGeneratedColumn="False"
和 ViewModel
的 DataGrid
,它绑定到我的 Window 的 DataContext
,其中 DataGrid
位于。
在我的 ViewModel
中,我有一个名为 Model
的数据结构列表,它绑定到我的 DataGrid
.
的 ItemsSource
属性
该模型包含 Enum
。我能够为枚举生成一个 DataGridComboboxColumn
,但我不知道如何为对象列表将其存档。
这是我的 Model.cs
class.
public class Model
{
public CType Type { get; set; }
public Person Person { get; set; }
public Condition Condition { get; set; }
}
public class Person
{
public List<Trait> Traits { get; set; }
public string Name { get; set; }
}
public class Condition
{
public int Id { get; set; }
public string Name { get; set; }
public int Value { get; set; }
}
public class Trait
{
public string Name { get; set; }
public List<Reason> Reasons { get; set; }
}
public class Reason
{
public string Name { get; set; }
}
这是我的 ViewModel.cs
class,其中包含类型为 Model
的预填充对象。
public class ViewModel
{
//Bind to Grid - ItemsSource
public ObservableCollection<Model> Models { get; set; }
//Possible ComboboxValues for Column: Model.Person
public ObservableCollection<Person> Persons { get; set; }
//Possible ComboboxValues for Column: Model.Condition
public ObservableCollection<Condition> Conditions { get; set; }
public ViewModel()
{
Models = new ObservableCollection<Model>();
#region Model1
Model model = new Model();
model.Condition = new Condition()
{
Id = 1,
Name = "Hallo",
Value = 5
};
model.Type = CType.One;
model.Person = new Person()
{
Name = "Peter",
Traits = new List<Trait>()
{
new Trait()
{
Name = "Trait 1",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 1" },
new Reason() { Name = "Reason 2" }
}
},
new Trait()
{
Name = "Trait 2",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 3" },
new Reason() { Name = "Reason 4" }
}
}
}
};
#endregion
#region Model2
Model model2 = new Model();
model2.Condition = new Condition()
{
Id = 1,
Name = "Welt",
Value = 5
};
model2.Type = CType.Three;
model2.Person = new Person()
{
Name = "Manuel",
Traits = new List<Trait>()
{
new Trait()
{
Name = "Trait 3",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 5" },
new Reason() { Name = "Reason 6" }
}
},
new Trait()
{
Name = "Trait 4",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 7" },
new Reason() { Name = "Reason 8" }
}
}
}
};
#endregion
Persons = new ObservableCollection<Person>()
{
model.Person, model2.Person
};
Conditions = new ObservableCollection<Condition>()
{
model.Condition, model2.Condition
};
Models.Add(model);
Models.Add(model2);
}
}
这是我的 Window.xaml
文件,其中包含工作枚举示例。
最后但同样重要的是,您将 DataContext
属性 设置为后面代码中 ViewModel
class 的新实例。
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="myEnumData"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:CType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Models}">
<DataGrid.Columns>
<DataGridComboBoxColumn
Header="Enum"
ItemsSource="{Binding Source={StaticResource myEnumData}, Mode=OneWay}"
SelectedValueBinding="{Binding Path=Type}">
</DataGridComboBoxColumn>
<!--ComboBox for Persons-->
<DataGridComboBoxColumn>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
试试这个:
<DataGridComboBoxColumn Header="Person" DisplayMemberPath="Name" SelectedItemBinding="{Binding Person}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.Persons, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.Persons, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
我在这个网站上找到了一些例子,但我找不到任何我可以用在我的例子中的东西。
我有一个带有 AutoGeneratedColumn="False"
和 ViewModel
的 DataGrid
,它绑定到我的 Window 的 DataContext
,其中 DataGrid
位于。
在我的 ViewModel
中,我有一个名为 Model
的数据结构列表,它绑定到我的 DataGrid
.
ItemsSource
属性
该模型包含 Enum
。我能够为枚举生成一个 DataGridComboboxColumn
,但我不知道如何为对象列表将其存档。
这是我的 Model.cs
class.
public class Model
{
public CType Type { get; set; }
public Person Person { get; set; }
public Condition Condition { get; set; }
}
public class Person
{
public List<Trait> Traits { get; set; }
public string Name { get; set; }
}
public class Condition
{
public int Id { get; set; }
public string Name { get; set; }
public int Value { get; set; }
}
public class Trait
{
public string Name { get; set; }
public List<Reason> Reasons { get; set; }
}
public class Reason
{
public string Name { get; set; }
}
这是我的 ViewModel.cs
class,其中包含类型为 Model
的预填充对象。
public class ViewModel
{
//Bind to Grid - ItemsSource
public ObservableCollection<Model> Models { get; set; }
//Possible ComboboxValues for Column: Model.Person
public ObservableCollection<Person> Persons { get; set; }
//Possible ComboboxValues for Column: Model.Condition
public ObservableCollection<Condition> Conditions { get; set; }
public ViewModel()
{
Models = new ObservableCollection<Model>();
#region Model1
Model model = new Model();
model.Condition = new Condition()
{
Id = 1,
Name = "Hallo",
Value = 5
};
model.Type = CType.One;
model.Person = new Person()
{
Name = "Peter",
Traits = new List<Trait>()
{
new Trait()
{
Name = "Trait 1",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 1" },
new Reason() { Name = "Reason 2" }
}
},
new Trait()
{
Name = "Trait 2",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 3" },
new Reason() { Name = "Reason 4" }
}
}
}
};
#endregion
#region Model2
Model model2 = new Model();
model2.Condition = new Condition()
{
Id = 1,
Name = "Welt",
Value = 5
};
model2.Type = CType.Three;
model2.Person = new Person()
{
Name = "Manuel",
Traits = new List<Trait>()
{
new Trait()
{
Name = "Trait 3",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 5" },
new Reason() { Name = "Reason 6" }
}
},
new Trait()
{
Name = "Trait 4",
Reasons = new List<Reason>()
{
new Reason() { Name = "Reason 7" },
new Reason() { Name = "Reason 8" }
}
}
}
};
#endregion
Persons = new ObservableCollection<Person>()
{
model.Person, model2.Person
};
Conditions = new ObservableCollection<Condition>()
{
model.Condition, model2.Condition
};
Models.Add(model);
Models.Add(model2);
}
}
这是我的 Window.xaml
文件,其中包含工作枚举示例。
最后但同样重要的是,您将 DataContext
属性 设置为后面代码中 ViewModel
class 的新实例。
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="myEnumData"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:CType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Models}">
<DataGrid.Columns>
<DataGridComboBoxColumn
Header="Enum"
ItemsSource="{Binding Source={StaticResource myEnumData}, Mode=OneWay}"
SelectedValueBinding="{Binding Path=Type}">
</DataGridComboBoxColumn>
<!--ComboBox for Persons-->
<DataGridComboBoxColumn>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
试试这个:
<DataGridComboBoxColumn Header="Person" DisplayMemberPath="Name" SelectedItemBinding="{Binding Person}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.Persons, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.Persons, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>