【发布时间】:2015-02-19 12:52:19
【问题描述】:
我知道以前有人问过这个问题,但我仍然无法让它工作 我只是在测试东西,因为我是新手,所以我没有使用 MVVM。 我有一个绑定到实体的可观察集合的数据网格 数据绑定工作正常,但我想在编辑模式下将一列设为 ComboBox 我实现了,但我不能不将该列绑定到实体的特定属性。
这是我的 Datagrid xaml:
<DataGrid x:Name="dataGrid1" IsSynchronizedWithCurrentItem="false" ItemsSource="{Binding}" RowHeaderWidth="0" HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Margin="50.752,160.516,0,0" VerticalAlignment="Top" Height="Auto" MaxHeight="200" Width="395.429" RenderTransformOrigin="0.5,0.5" Background="#FFCFCFCF" ColumnWidth="*" HorizontalGridLinesBrush="Black" VerticalGridLinesBrush="Black" RowBackground="#FFCFCFCF" AreRowDetailsFrozen="True" Style="{DynamicResource DataGridStyle2}" CellEditEnding="dataGrid1_CellEditEnding" CurrentCellChanged="dataGrid1_CurrentCellChanged" Grid.Column="1" Opacity="0" SelectionChanged="dataGrid1_SelectionChanged" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Block.TextAlignment" Value="Center"/>
<Setter Property="Template" Value="{DynamicResource DataGridCellControlTemplate1}"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.Effect>
<DropShadowEffect BlurRadius="20"/>
</DataGrid.Effect>
<DataGrid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</DataGrid.RenderTransform>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryFont}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns >
<DataGridTemplateColumn x:Name="champ_delete" d:IsHidden="True" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Button x:Name="delete" Content="Button" Width="30" Height="30" Style="{DynamicResource ButtonStyle12}" Click="supprime_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这是我的 MainWindow.xaml.cs 代码
/// <summary>
/// Interaction logic for MENU.xaml
/// </summary>
public partial class MENU : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MENU()
{
this.InitializeComponent();
SContext = new Entities();
ObsCollection = new ObservableCollection<PERFCONTENEUR>(SContext.PERFCONTENEUR.ToList());
DataContext = this;
dataGrid1.ItemsSource = ObsCollection;
}
public BindingList<PERFCONTENEUR> Ji { get; set; }
public ObservableCollection<PERFCONTENEUR> ObsCollection { get; set; }
public Entities SContext;
private void dataGrid1_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "CLIENT")
{
var cb = new DataGridComboBoxColumn();
e.Column.Header = "clients";
cb.ItemsSource = (DataContext as ObservableCollection<PERFCONTENEUR>).ToList(); // I think the problem is here
cb.SelectedValueBinding = new Binding("CLIENT");
e.Column = cb;
}
}
}
【问题讨论】:
-
我不关注。您想将 cb.ItemsSource 绑定到什么?
-
@blam 有一个名为 CLIENT 的字段我想在组合框中显示来自数据库的当前值
-
我的问题是 ItemsSource 不是当前值。作为 ObservableCollection PERFCONTENEUR,您希望从 DataContext 中获得什么?该数据上下文不会转换为 ObservableCollection。
标签: c# wpf data-binding datagridcomboboxcolumn