gpt4 book ai didi

c# - DataGrid 每一行中的组合框

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:25 25 4
gpt4 key购买 nike

我正在制作一个与 SSIS 执行相同工作的 WPF 应用程序,下一步我想将我的源列映射到数据库表列。我想实现这样的目标 enter image description here

谁能帮我找到一种将组合框绑定(bind)到数据网格的方法。我正在使用 MVVM 设计模式。

最佳答案

当您在DataGridComboBoxColumn 上单击鼠标时,您可以看到ComboBox。让我们看一个例子:

型号:

public class Person
{
public int IdPerson { get; set; }
public string Name { get; set; }
}

public class Team
{
public int IdTeam { get; set; }
public string NameTeam { get; set; }
}

View 模型:

public class MainWindowVM
{
public MainWindowVM()
{
LoadData();
}

private void LoadData()
{
Persons = new ObservableCollection<Person>() {
new Person() { IdPerson = 1, Name = "Billy" },
new Person() { IdPerson = 2, Name = "Bobby" },
new Person() { IdPerson = 2, Name = "Bond" } };

Teams = new ObservableCollection<Team>() {
new Team() { IdTeam = 1, NameTeam = "Team A" },
new Team() { IdTeam = 2, NameTeam = "Team B" },
new Team() { IdTeam = 3, NameTeam = "Team C" } };
}


public ObservableCollection<Person> Persons { get; set; }
public ObservableCollection<Team> Teams { get; set; }
}

查看:

<Window x:Class="WPFApplication.MainWindow"
...the code omitted for the brevity
xmlns:vm="clr-namespace:WPFApplication.ViewModel"
Title="a" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<vm:MainWindowVM/>
</Window.DataContext>
<Grid>
<StackPanel>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />

<DataGridComboBoxColumn Header="CourtType"
DisplayMemberPath="NameTeam" SelectedValueBinding="{Binding IdTeam}" SelectedValuePath="{Binding IdTeam}"
>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Teams}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Teams}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

<DataGridTextColumn Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>

关于c# - DataGrid 每一行中的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35080582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com