DataGridComboBoxColumn绑定到DataGrid DataContext

问题描述 投票:0回答:1

我快要放弃了。我一直在尝试将这个DataGridComboBoxColumn绑定到DataGrid的DataContext(一个DataSet),但我不能。代码如下所示:

UserControl C#:

private static String strDBPath = EMS.Properties.Settings.Default.DBFile;
private static OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDBPath + ";Jet OLEDB:Engine Type=5");
private static string strProp = "SELECT * FROM tblProperties";
private static OleDbDataAdapter adapterProp = new OleDbDataAdapter(strProp, myConn);
private static DataSet dsProp = new DataSet();
public ObservableCollection<string> VarTypes { get; set; }

public UserControlPropertiesAccess() {
     VarTypes = new ObservableCollection<string>() { "N", "Bool", "A2", "A4", "A8", "A20", "A50", "A100", "A200", };

     InitializeComponent();
     DatabaseHandling.CreateTable("tblProperties");
     adapterProp.Fill(dsProp, "LoadDataBindingProp");
     dgProperties.DataContext = dsProp;
}

在dsProp中,我有以下字段:PropID,PropName,PropVarType和PropUnit。我使用绑定来显示我的DataGrid中其他列中的最后三个字段,一切正常。当我尝试使用DataGridComboBoxColumn时,它只是不绑定到DataContext。我只设法将我的ObservableCollection作为我的组合框项目。

XAML:

    <DataGrid Name="dgProperties" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightCyan" Style="{StaticResource AzureDataGrid}" AutoGenerateColumns="False" CanUserAddRows="False" SelectionMode="Single" ItemsSource="{Binding Path=LoadDataBindingProp}" IsReadOnly="True" SelectionChanged="dgProperties_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=PropName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Property" Width="150" />
            <DataGridComboBoxColumn x:Name="dgcmbProperties" SelectedItemBinding="{Binding Path=PropName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Variable" Width="80" />
             <DataGridTextColumn Binding="{Binding Path=PropUnit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Unit" Width="100" />
        </DataGrid.Columns>
    </DataGrid>

用户完成编辑后,我使用以下方法将数据上传到我的数据库:

    private void btnSaveProperty_Click(object sender, RoutedEventArgs e)
    {
        myConn.Open();
        adapterProp.Update(dsProp.Tables[0]);
        myConn.Close();
    }

所以我都希望将我的DataGridComboBoxColumn绑定到我的DataSet。此外,所选项目未显示在DataGrid单元格中。

任何帮助将非常感谢。

c# wpf xaml datagrid datagridcomboboxcolumn
1个回答
0
投票

您没有为DataGridComboBoxColumn设置ItemsSource

我想你的主视图模型(主窗口的DataContext)有一个包含所有组合框项目的集合(如MyComboItems)

那应该工作:

<Window x:Name="root">
    <DataGrid ItemsSource="{Binding Path=LoadDataBindingProp}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="dgcmbProperties" Header="Variable"
                  ItemsSource="{Binding 
                      ElementName=root, 
                      Path=DataContext.MyComboItems}"
                  SelectedItemBinding="{Binding 
                      Path=PropName, 
                      Mode=TwoWay, 
                      UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.