如何用静态项填充数据网格中的组合框
本文关键字:网格 组合 数据网 数据 何用 静态 填充 | 更新日期: 2023-09-27 18:14:21
我正在开发wpf应用程序,但不使用MVVM架构。我在数据网格中有一个组合框,我静态地填充它的itemssource。这在我的电脑上工作得很好,但是当我在新电脑上安装应用程序时,组合框的itemssource是空的。
CommandeWindow.xaml
<DataGrid x:Name="dataGridCommand" ItemsSource="{Binding Path=ProdCommande, ElementName=CommandeWindow,Mode=TwoWay}" AutoGenerateColumns="False" TextOptions.TextFormattingMode="Display" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" SelectionMode="Single" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn x:Name="prodReference" CanUserReorder="False" Binding="{Binding refProduit}" Width="150" Header="Référence Paulstra"/>
<DataGridTextColumn x:Name="prodCDP" CanUserReorder="False" Binding="{Binding cdp}" Width="50" Header="CDP"/>
<DataGridTemplateColumn Header="Statut du commande" CanUserReorder="False" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding StatutCommande, ElementName=CommandeWindow}" SelectedItem="{Binding statut, Mode=TwoWay}" SelectionChanged="ComboBox_SelectionChanged" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这是我在
后面的代码CommandeWindow.xaml.cs
public object StatutCommande
{
get { return GetValue(StatutComProperty); }
set { SetValue(StatutComProperty, value); }
}
// Using a DependencyProperty as the backing store for ACCOU. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StatutComProperty =
DependencyProperty.Register("StatutCommande", typeof(object), typeof(Commande), new UIPropertyMetadata(null));
public Commande()
{
context = new CRM_HUTCHINSONEntities();
StatutCommande = new List<string> { "En cours", "Commandée", "Perdue" };
...
我知道怎么解决它了。我只是添加RelativeSource={RelativeSource祖先类型=窗口}到我的itemssource在组合框
<DataGrid x:Name="dataGridCommand" ItemsSource="{Binding Path=ProdCommande, ElementName=CommandeWindow,Mode=TwoWay}" AutoGenerateColumns="False" TextOptions.TextFormattingMode="Display" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" SelectionMode="Single" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn x:Name="prodReference" CanUserReorder="False" Binding="{Binding refProduit}" Width="150" Header="Référence Paulstra"/>
<DataGridTextColumn x:Name="prodCDP" CanUserReorder="False" Binding="{Binding cdp}" Width="50" Header="CDP"/>
<DataGridTemplateColumn Header="Statut du commande" CanUserReorder="False" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding StatutCommande, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding statut, Mode=TwoWay}" SelectionChanged="ComboBox_SelectionChanged" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>