我正在研究wpf应用程序,但没有使用MVVM架构。我在datagrid中有一个组合框,我静态填充其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" };
...
答案 0 :(得分:0)
我弄清楚如何解决它。我只是将RelativeSource = {RelativeSource AncestorType = Window}添加到我在combobox中的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>