且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示,选择在DataGrid中的项目组合框与WPF C#,使用MVVM

更新时间:2022-04-24 07:54:32

据我可以理解,问题是如何在一定XAML资源绑定为一个组合的ItemsSource在addtion如何组合的选定值binnd的DataGrid行背后的模型
1.列表项:

As I can understand the problem is how to bind some XAML resource as a combo ItemsSource and in addtion how to binnd the selected value of a combo to the model behind the DataGrid row. 1. List item:

<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. 数据网格视图模型:

  1. DataGrid viewmodel:

public class DataGridMainDataContext

{
公共DataGridMainDataContext()
{
系列=新的ObservableCollection(新表
{
新的UserData
{
的uname =格雷格,
IsActive =假,
},
新的UserData
{
的uname = 乔,
IsActive =假,
},
新的UserData
{
的uname =IV,
IsActive =假,
}
});

{ public DataGridMainDataContext() { Collection = new ObservableCollection(new List { new UserData { UName = "Greg", IsActive = false, }, new UserData { UName = "Joe", IsActive = false, }, new UserData { UName = "Iv", IsActive = false, } });

}

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



}

}

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

Models: public class UserData : BaseObservableObject { private string _uName; private object _countryData; private bool _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 class ActivityStatus:BaseObservableObject { private bool _boolStatus; private string _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 class Country : BaseObservableObject { private string _countryName; private string _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;
}



}
希望它会帮助你。

} Hope it will help you.

问候,