2015-10-21 38 views
0

我希望能夠從wpf Datagrid內的組合框中選擇「true」或「false」(布爾值),並且能夠將該選項保存到我的數據庫中。如何使用MVVM顯示和選擇具有WPF C#的Datagrid組合框中的項目使用MVVM

我希望能夠通過保存到我的數據庫的布爾變量(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。我是一個新手:-(。

+0

是您ACCOUNTTYPE實現了INotifyPropertyChanged的上綁定化子性質? DataGrid ItemsSource集合中有數據嗎? – Ilan

+0

嗨。是的,我在我的CommandBase類中實現了INotifyPropertyChanged。我確實有虛擬數據,除了ComboBox之外,它顯示在所有其他列中。我的Commandbase是一個接口類,並被所有ViewModel使用。 Collection Itemsource是一個Observable集合。 – Newbie101

+0

這裏沒有Combo ItemsSource綁定。你是否在代碼背後設置了它? – Ilan

回答

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> 

  • 數據網格視圖模型:

    public class DataGridMainDataContext 
    

    { 公共DataGridMainDataContext() { 類別=新的ObservableCollection(新列表 { new UserData { UNAME = 「格雷格」, IsActive =假, }, 新的UserData { UNAME = 「喬」, IsActive =假, }, 新的UserData { UNAME = 「IV」, IsActive =假, } });

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

    }

  • 型號: 公共類的UserData:BaseObservableObject { 私人字符串_uName; 私人對象_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 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; 
    } 
    

    } 希望它能幫助你。

  • 問候,

    +0

    @ Newbie101歡迎您。我很高興幫助,隨時將其標記爲回答,如果有幫助。 – Ilan

    +0

    我會的。我還有一個問題。是否有替代使用SelectedValueBinding DataGridComboBoxColumn的屬性?我問,因爲我使用VS2013,它不被識別,或者它可能無法訪問,如果是這樣的話,它的命名空間是什麼?如果我能解決這個問題,我們將解決這個問題。 – Newbie101

    +0

    // @ Newbie101很奇怪。我正在VS2013上工作,而且我沒有任何這方面的專業知識。你有什麼樣的.Net網站? – Ilan

    相關問題