【问题标题】:DataGridComboBoxColumn binding to DataGrid DataContextDataGridComboBoxColumn 绑定到 DataGrid DataContext
【发布时间】:2015-01-21 02:09:04
【问题描述】:

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

用户控件 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 绑定到我的数据集。此外,所选项目未显示在 DataGrid 单元格中。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf xaml datagrid datagridcomboboxcolumn


    【解决方案1】:

    您没有为 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>
    

    【讨论】:

      猜你喜欢
      • 2014-01-04
      • 2012-11-22
      • 2013-11-16
      • 1970-01-01
      • 2017-08-05
      • 2013-12-12
      • 1970-01-01
      • 2012-02-12
      • 2010-11-01
      相关资源
      最近更新 更多