Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF绑定列表到datagrid可编辑组合框列_C#_Wpf - Fatal编程技术网

C# WPF绑定列表到datagrid可编辑组合框列

C# WPF绑定列表到datagrid可编辑组合框列,c#,wpf,C#,Wpf,因此,目前我可以从代码中将对象列表绑定到组合框列,但我很难使其可编辑 请参阅下面的代码 我的Xaml <DataGridComboBoxColumn x:Name="dgEmpcmbName" SelectedValueBinding="{Binding ID, UpdateSourceTrigger=LostFocus}" ClipboardContentBinding="{x:Null}" Header="Employee name" Width="Auto"/> 如果有人知道

因此,目前我可以从代码中将对象列表绑定到组合框列,但我很难使其可编辑

请参阅下面的代码

我的Xaml

<DataGridComboBoxColumn x:Name="dgEmpcmbName" SelectedValueBinding="{Binding ID, UpdateSourceTrigger=LostFocus}" ClipboardContentBinding="{x:Null}" Header="Employee name" Width="Auto"/>
如果有人知道如何使其可编辑,或者只是在SelectionChanged上添加一个事件,那就太棒了

谢谢

编辑:


顺便说一句,这不是重复的,因为我已经看到了datagrid模板列的解决方案,但是当我使用模板列时,我无法从代码绑定列表。。(我不是说这是不可能的,我是说我不能…。如果你能提供帮助:)

通过创建一个模板列并在数据模板中创建组合框,使用绑定,以不同的方式设置列

这是基于以下答案得出的:


编辑: 我想这就是你要找的。在本例中,我有一个绑定到XAML表单的viewmodel

视图模型:

public class MainWindowViewModel 
{
    public MainWindowViewModel()
    {
        GridItems = new ObservableCollection<GridItem>() {
        new GridItem() { Name = "Chef", PeopleId = 1 } };

        PeopleItems = new ObservableCollection<PeopleItem>() {
        new PeopleItem() { ID = 1, Name = "George" },
        new PeopleItem() { ID = 2, Name = "Martha" } };
    }

    public ObservableCollection<GridItem> GridItems { get; set; }
    public static ObservableCollection<PeopleItem> PeopleItems { get; set; }
}
public class GridItem
{
    public string Name { get; set; }
    public int PeopleId { get; set; }
}

public class PeopleItem
{
    public int ID { get; set; }
    public string Name { get; set; }
}
公共类MainWindowViewModel
{
公共主窗口视图模型()
{
GridItems=新的ObservableCollection(){
新建GridItem(){Name=“Chef”,PeopleId=1};
PeopleItems=新的ObservableCollection(){
new PeopleItem(){ID=1,Name=“George”},
newpeopleItem(){ID=2,Name=“Martha”};
}
公共ObservableCollection GridItems{get;set;}
公共静态ObservableCollection PeopleItems{get;set;}
}
公共类GridItem
{
公共字符串名称{get;set;}
public int PeopleId{get;set;}
}
公共类人物项目
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
然后是您的XAML表单:

<Window x:Class="WpfApp.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:WpfApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    >
<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding GridItems}" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" />
        <DataGridComboBoxColumn
ItemsSource="{Binding Source={x:Static local:MainWindowViewModel.PeopleItems }}" 
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValueBinding="{Binding CompanyID}" />

    </DataGrid.Columns>
</DataGrid>


使用DataGridTemplateColumn,我将这样做。我发现了一个我大量借用的例子,

但是我如何通过xaml绑定我的ItemsSource,因为我的列表在我的cs文件中?顺便说一句,谢谢你的帮助assisting@Hancs什么是绑定到xaml的viewmodel?你能发布吗?代码隐藏->数据处理程序->类->接口(如果我误解了,请原谅,我还是一名二年级学生,现在只学习基础知识。)基本上我想知道的是如何将人员列表绑定到UserControl加载事件上的组合框模板
public class MainWindowViewModel 
{
    public MainWindowViewModel()
    {
        GridItems = new ObservableCollection<GridItem>() {
        new GridItem() { Name = "Chef", PeopleId = 1 } };

        PeopleItems = new ObservableCollection<PeopleItem>() {
        new PeopleItem() { ID = 1, Name = "George" },
        new PeopleItem() { ID = 2, Name = "Martha" } };
    }

    public ObservableCollection<GridItem> GridItems { get; set; }
    public static ObservableCollection<PeopleItem> PeopleItems { get; set; }
}
public class GridItem
{
    public string Name { get; set; }
    public int PeopleId { get; set; }
}

public class PeopleItem
{
    public int ID { get; set; }
    public string Name { get; set; }
}
<Window x:Class="WpfApp.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:WpfApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    >
<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding GridItems}" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" />
        <DataGridComboBoxColumn
ItemsSource="{Binding Source={x:Static local:MainWindowViewModel.PeopleItems }}" 
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValueBinding="{Binding CompanyID}" />

    </DataGrid.Columns>
</DataGrid>