WPF Datagrid模板Combobox在数据网格的其余部分时不显示数据

时间:2014-09-09 03:55:53

标签: c# wpf xaml datagrid combobox

我有一个简单的SQL数据库,其中包含EF模型和设置为模型表对象的数据集。如果我从数据源中的“Customers”表中拖放datagid,一切正常。但是如果我在拖放数据网格之前将“状态”字段更改为组合框,我无法让组合框显示来自数据库的数据,无论我尝试什么。 我将组合框的itemsource设置为app.xaml中Application.Resources中定义的静态资源列表,如下所示: -

<Application.Resources>
<x:Array x:Key="StateListString" Type="local:ComboBoxItemString">
    <local:ComboBoxItemString ValueString = "ACT"/>
    <local:ComboBoxItemString ValueString = "QLD"/>
    <local:ComboBoxItemString ValueString = "NSW"/>
    <local:ComboBoxItemString ValueString = "NT"/>
    <local:ComboBoxItemString ValueString = "SA"/>
    <local:ComboBoxItemString ValueString = "TAS"/>
    <local:ComboBoxItemString ValueString = "VIC"/>
    <local:ComboBoxItemString ValueString = "WA"/>
</x:Array>

我的XAML页面是 -

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:Generic="clr-namespace:System.Collections.Generic;assembly=mscorlib"
  xmlns:local="clr-namespace:AthenaArt" x:Class="AthenaArt.CustomerPage" 
  mc:Ignorable="d" 
  d:DesignHeight="650" d:DesignWidth="1200"
Title="CustomerPage" Loaded="Page_Loaded">
<Page.Resources>
    <CollectionViewSource x:Key="customerViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Customer}, CreateList=True}"/>
</Page.Resources>

<Grid DataContext="{StaticResource customerViewSource}">
    <DataGrid x:Name="customerDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <!--DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/-->
            <DataGridTextColumn x:Name="firstNameColumn" Binding="{Binding FirstName}" Header="First Name" Width="80"/>
            <DataGridTextColumn x:Name="lastNameColumn" Binding="{Binding LastName}" Header="Last Name" Width="80"/>
            <DataGridTextColumn x:Name="companyNameColumn" Binding="{Binding CompanyName}" Header="Company Name" Width="120"/>
            <DataGridTextColumn x:Name="addressColumn" Binding="{Binding Address}" Header="Address" Width="120"/>
            <DataGridTextColumn x:Name="address2Column" Binding="{Binding Address2}" Header="Address 2" Width="120"/>
            <DataGridTextColumn x:Name="suburbColumn" Binding="{Binding Suburb}" Header="Suburb" Width="110"/>
            <DataGridTemplateColumn x:Name="stateColumn" Header="State" Width="80">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                       <ComboBox x:Name="cboState" Initialized="cbo_Initialized"
                                  ItemsSource ="{StaticResource StateListString}"  
                                  IsEditable="False"
                                  SelectedValue="{Binding State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="ValueString"
                                  IsSynchronizedWithCurrentItem = "False"
                                  SelectedValuePath ="ValueString" 
                                  Width="Auto">
                        </ComboBox>
                     </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="postCodeColumn" Binding="{Binding PostCode}" Header="Post Code" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="emailColumn" Binding="{Binding Email}" Header="Email" Width="150"/>
            <DataGridTextColumn x:Name="phoneColumn" Binding="{Binding Phone}" Header="Phone" Width="90"/>
            <DataGridTextColumn x:Name="mobileColumn" Binding="{Binding Mobile}" Header="Mobile" Width="90"/>
            <DataGridTextColumn x:Name="notesColumn" Binding="{Binding Notes}" Header="Notes" Width="100"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

我的代码背后是 -

namespace AthenaArt
{ 
    /// <summary>
    /// Interaction logic for CustomerPage.xaml
    /// </summary>
    public partial class CustomerPage : Page //This page is loaded into a ribbon window on the Main Window
    {
        private Athena_ArtEntities _context = new Athena_ArtEntities();

        public CustomerPage()
        {
           InitializeComponent();
        }

        internal void SaveCustomer()//Called from a button on the main window
        {
           //Commit the newly entered row
            this.customerDataGrid.CommitEdit();
           _context.SaveChanges();

            // Refresh the grid so the database generated values show up. All do except the combobox values
            this.customerDataGrid.Items.Refresh();
        }

        internal void AddCustomer()
        {
          // TODO
        }

        internal void LoadData()
        {
             System.Windows.Data.CollectionViewSource customerViewSource =
           ((System.Windows.Data.CollectionViewSource)(this.FindResource("customerViewSource")));

            _context.Customers.Load();

            customerViewSource.Source = _context.Customers.Local; 
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        { 
            LoadData();
        }   
    }
}

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using AthenaArt; 
namespace AthenaArt 
{ 
    public class ComboBoxItemString 
    { 
        public string ValueString { get; set; } 
    } 
} 

组合框字段的更改会正确写回数据库,并且通过在SelectedValue绑定上实现UpdateSourceTrigger = PropertyChanged,保存网格后值不会消失。但是,加载网格时不会填充值。我现在正在撕扯我的头发,我真的很感激一些帮助。

0 个答案:

没有答案