【问题标题】:How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM如何使用 MVVM 在带有 WPF C# 的 Datagrid ComboBox 中显示和选择项目
【发布时间】:2016-01-20 04:21:49
【问题描述】:

我希望能够从 wpf Datagrid 中的 ComboBox 中选择“true”或“false”(布尔值),并能够将该选择保存到我的数据库中。

我希望能够通过保存到我的数据库中的布尔变量作为位(1 = true; 0 = false)来指示该行是否处于“活动”状态。

这是我的创建表语句: Create Table Statement(AccountType)

我使用 ObservableCollection 填充数据网格,如下所示:

protected override void Get()
{
    using (var dbContext = new IEMASEntitiesDataContext())
    {
        var accountType = from s in dbContext.AccountTypes select s;
        var observable = new  ObservableCollection<AccountType>(accountType);

        Collection = observable;
    }
}

我的 XAML 代码如下:

<DockPanel DataContext="{StaticResource ResourceKey=AccountTypeViewModel}" LastChildFill="True">
    <ToolBar DockPanel.Dock="Top">
        <Button Content="Display"  Command="{Binding GetCommand}" Width="78"/>
        <Button Content="Save" Command="{Binding SaveCommand}" Width="78"/>
    </ToolBar>

    <Grid>
        <GroupBox x:Name="AccountTypeGroupBox">
            <DataGrid x:Name="DataGridAccountType" ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn  Header="AccountType" Width="150" Binding="{Binding Path=AccountTypeName, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                    <DataGridComboBoxColumn  Header="Active" Width="100" SelectedValueBinding="{Binding StatusList, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="AccountTypeId" DisplayMemberPath="Active"/>                   
                    <DataGridTemplateColumn Width="50" Header="Delete">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="btnDelete" Content="Delete" Command="{Binding DeleteCommand}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>          
    </Grid>
</DockPanel>

它不起作用。组合框中没有显示任何内容,并且我不知道在显示时如何将所选项目保存到 SQL Server 数据库中。我会很感激一些帮助,请。我正在使用 LINQ TO SQL。我是新手:-(。

【问题讨论】:

  • 您的 AccountType 是否在绑定属性上实现了 INotifyPropertyChanged? DataGrid ItemsSource 集合中是否有数据?
  • 嗨。是的,我在我的 CommandBase 类中实现了 INotifyPropertyChanged。我确实有虚拟数据,它显示在所有其他列中,除了 ComboBox。我的 Commandbase 是一个接口类,我的所有 ViewModel 都在使用它。 Collection Itemsource 是一个 Observable 集合。
  • 那里没有 Combo ItemsSource 绑定。你是在代码后面设置的吗?
  • 我选择了ListView的路线。让我告诉你我的意思:我有一个 GridViewColumn.Cellplate。而它的DataTemplate 的子对象就是ComboBox。我希望它填充两个项目:True 和 False。我希望能够从这两个选项中进行选择:ItemSource 将绑定到数据库中表的 Active 列。请给我一个例子,说明你将如何做到这一点。我希望能够检索选择并保存它。我会根据需要修改代码,如果可以请演示一下。

标签: c# wpf mvvm combobox datagrid


【解决方案1】:

据我所知,问题是如何将某些 XAML 资源绑定为组合 ItemsSource,以及如何将组合的选定值绑定到 DataGrid 行后面的模型。 1. 列表项:

<Window x:Class="SoDataGridProjectsHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:soDataGridProjectsHelpAttempt="clr-namespace:SoDataGridProjectsHelpAttempt"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <x:Array x:Key="CountriesArray" Type="soDataGridProjectsHelpAttempt:Country">
        <soDataGridProjectsHelpAttempt:Country CountryName="Germany" CountryPop="150k"/>
        <soDataGridProjectsHelpAttempt:Country CountryName="France" CountryPop="125k"/>
        <soDataGridProjectsHelpAttempt:Country CountryName="Belarus" CountryPop="165k"/>
    </x:Array>
    <x:Array x:Key="StatusArray" Type="soDataGridProjectsHelpAttempt:ActivityStatus">
        <soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="Yes" BoolStatus="True"/>
        <soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="No" BoolStatus="False"/>
    </x:Array>
</Window.Resources>
<Window.DataContext>
    <soDataGridProjectsHelpAttempt:DataGridMainDataContext/>
</Window.DataContext>
<Grid>
    <DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False" CanUserAddRows="True">
        <DataGrid.Columns>
            <DataGridTextColumn     Width="Auto" Binding="{Binding UName}"/>
            <DataGridComboBoxColumn Header="Country" DisplayMemberPath="CountryName"
                                    ItemsSource="{StaticResource CountriesArray}" Width="Auto"
                                    SelectedItemBinding="{Binding CountryData}"/>
            <!--<DataGridComboBoxColumn Header="ActivityStatus" Width="Auto" ItemsSource="{StaticResource StatusArray}" 
                                    SelectedValueBinding="{Binding IsActive}" SelectedValuePath="BoolStatus" DisplayMemberPath="VerbalStatus"/>-->
            <DataGridComboBoxColumn Header="ActivityStatus" SelectedItemBinding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <DataGridComboBoxColumn.ItemsSource>
                    <x:Array Type="system:Boolean">
                        <system:Boolean>True</system:Boolean>
                        <system:Boolean>False</system:Boolean>
                    </x:Array>
                </DataGridComboBoxColumn.ItemsSource>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

  1. DataGrid 视图模型:

    public class DataGridMainDataContext
    

    { 公共 DataGridMainDataContext() { 集合 = 新的 ObservableCollection(新列表 { 新用户数据 { UName = "格雷格", IsActive = 假, }, 新用户数据 { UName = "乔", IsActive = 假, }, 新用户数据 { UName = "IV", IsActive = 假, } });

    }
    
    public ObservableCollection<UserData> Collection { get; set; }
    

    }

  2. 型号: 公共类 UserData : BaseObservableObject { 私有字符串_uName; 私有对象 _countryData; 私人布尔_isActive;

    public bool IsActive
    {
        get { return _isActive; }
        set
        {
            _isActive = value;
            OnPropertyChanged();
        }
    }
    
    public string UName
    {
        get { return _uName; }
        set
        {
            _uName = value;
            OnPropertyChanged();
        }
    }
    
    public object CountryData
    {
        get { return _countryData; }
        set
        {
            _countryData = value;
            OnPropertyChanged();
        }
    }
    

    }

    公共类 ActivityStatus:BaseObservableObject { 私人布尔 _boolStatus; 私有字符串_verbalStatus;

    public bool BoolStatus
    {
        get { return _boolStatus; }
        set
        {
            _boolStatus = value;
            OnPropertyChanged();
        }
    }
    
    public string VerbalStatus
    {
        get { return _verbalStatus; }
        set
        {
            _verbalStatus = value;
            OnPropertyChanged();
        }
    }
    

    }

    公共类国家:BaseObservableObject { 私人字符串 _countryName; 私有字符串_countryPop;

    public string CountryName
    {
        get { return _countryName; }
        set
        {
            _countryName = value;
            OnPropertyChanged();
        }
    }
    
    public string CountryPop
    {
        get { return _countryPop; }
        set
        {
            _countryPop = value;
            OnPropertyChanged();
        }
    }
    
    public Country() { }
    public Country(string n, string d)
    {
        this.CountryName = n;
        this.CountryPop = d;
    }
    

    } 希望对你有帮助。

问候,

【讨论】:

  • @Newbie101 不客气。我很乐意提供帮助,如果有帮助,请随时将其标记为已回答。
  • 我会的。我还有一个问题。是否可以将 SelectedValueBinding 用于 DataGridComboBoxColumn 的属性?我问是因为我使用的是 VS2013 并且它无法识别或可能无法访问,如果是这种情况,它的命名空间是什么?如果我能解决这个问题,我们就会解决这个问题。
  • @Newbie101 很奇怪。我正在研究VS2013,对此我没有任何问题。你有什么版本的.Net?
  • @Newbie101 试试这个,它一定有帮助。 SelectedValueBinding 中没有使用。
  • 问题现已正式解决。你是这里唯一的人,帮助我。世界其他地方看到它,点击通过它。要么他们真的没有答案,要么只是不想努力向我解释这个问题。我无法表达我的感激之情。我会将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 2014-05-17
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
相关资源
最近更新 更多