微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将字典绑定到DataGridComboBoxColumn WPF

Helo家伙..

我的想法是使用DataGrid作为映射器模板,首先网格将加载数据,让我们说“表A”,其中一个列将显示数据,让我们说“表B”

我有一个像这样的“表B”:

fieldtype_id | fieldtype_name
     1             int
     2             varchar
     3             date

我想使用DataGridComboBoxColumn在wpf Datagrid中显示此表.

所以,我在其中创建了一个wpf窗口和datagrid,下面是XAML

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="12,123,176" Name="dataGrid1" Width="1000" 
              ItemsSource="{Binding}" SelectionUnit="CellOrRowHeader" CanUserAddRows="False" 
              CellEditEnding="dataGrid1_CellEditEnding" CurrentCellChanged="dataGrid1_CurrentCellChanged">

        <DataGrid.Columns>

            <DataGridComboBoxColumn
                             Header="Field Type" Width="200" 
                             displayMemberPath="Value"
                             SelectedValueBinding="{Binding fieldtypeSS,Mode=TwoWay}"
                             SelectedValuePath="{Binding fieldtype_id}">

                <DataGridComboBoxColumn.ElementStyle>
                    <Style targettype="ComboBox">
                        <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
                        <Setter Property="ItemsSource" Value="{Binding Path=fieldtype_id}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>

                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style targettype="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=fieldtype_id}" />
                        <Setter Property="IsDropDownopen" Value="True" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>


        </DataGrid.Columns>

</DataGrid>

然后,我创建一个类:

public class cMapHeader
{
    public int fieldtypeSS { get; set; }
    public Dictionary<string,string> fieldtype_id { get; set; }
}

然后,使用我之前创建的方法填充表

//## Get MysqLFieldType
//## dboperation.dtQueries is my method to populate data,and return as DataTable
Dictionary<string,string> MysqLFieldType = new Dictionary<string,string>();

foreach (DaTarow row in dboperation.dtQueries("SELECT fieldtype_id,fieldtype_name FROM MysqL_Fieldtype ").Rows)
{
    MysqLFieldType.Add(row["fieldtype_id"].ToString(),row["fieldtype_name"] as string);
}

接下来,填充网格

gridMapHeader = new ObservableCollection<cMapHeader>()
        {
           new cMapHeader(){fieldtypeSS="1",fieldtype_id=MysqLFieldType},new cMapHeader(){fieldtypeSS="2",fieldtype_id=MysqLFieldType}

        };
dataGrid1.BeginInit();

dataGrid1.SetBinding(ItemsControl.ItemsSourceProperty,new Binding
      {
         Source = gridMapHeader
      });

问题是,应该包含fieldtypeSS的单元格没有显示,但是在编辑模式下,DataGridComboBoxColumn显示我想要的值(仅显示“表B”的fieldtype_name).

那么第二个问题是,当单元格失去焦点时,从DataGridComboBoxColumn中选择的值将丢失.

谁能帮我?

非常感谢任何帮助:D

Thx b4

解决方法

解决您的两个问题,您的:

SelectedValuePath=”{Binding fieldtype_id}”

应该:

SelectedValuePath=”fieldtype_id”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐