使用ItemsSource时,操作无效。但我不是在搅拌

时间:2015-12-20 00:30:45

标签: c# wpf entity-framework listbox

我有这个问题,我一直在寻找解决方案,我读了所有其他相关的帖子,但没有一个类似我的情景。 我的代码是一个程序,显示(并且只有)食品工厂中正在处理的产品数量

 <Window.Resources>
    <DataTemplate x:Key="ProductTemplate">
        <Grid d:DesignWidth="551.412" d:DesignHeight="54.172">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="139"/>
                <ColumnDefinition Width="Auto" MinWidth="53"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label x:Name="ReadyAvailable"  VerticalContentAlignment="Center" FontSize="26.667" Margin="0,0,0,0.172" Grid.Column="1" Content="{Binding Ready}"/>
            <ListBox x:Name="NextAvailable" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0.172" ItemsSource="{Binding NextAvailable}" ItemTemplate="{DynamicResource NextAvailableTemplate}">
                <ListBox.DataContext>
                    <local:Cooks/>
                </ListBox.DataContext>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox>
            <Label x:Name="ProductName" Content="{Binding CookingProduct.ShortName}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" Margin="0,0,0,0.172" FontSize="21.333" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="NextAvailableTemplate">
        <Grid d:DesignWidth="131.834" d:DesignHeight="54.172" Height="43">
            <Label x:Name="CounterString" Content="{Binding TimerString}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" FontSize="21.333">
                <Label.DataContext>
                    <local:Cooks/>
                </Label.DataContext>
            </Label>
        </Grid>
    </DataTemplate>
</Window.Resources>
<ListBox x:Name="lbCooks" ItemsSource="{Binding}" ItemTemplate="{DynamicResource ProductTemplate}" SelectionChanged="lbCook_SelectionChanged" HorizontalContentAlignment="Stretch">
    <ListBox.DataContext>
        <local:ProcessedProduct/>
    </ListBox.DataContext>
</ListBox>

如您所见,列表框列出了产品,名称,已生成和可用的数量,以及下一个待完成和可用的列表框产品。

我使用Entity Framwork 6并且我调用的ProcessingProduct对象正是与父列表框链接的对象

public class ProcessingProduct
{
    public FullList Product { get; set; }
    public int Ready { get; set; }
    public ObservableCollection<Cooks> NextAvailable { get; set; }
}

FullListCooks和任何其他非自然属性是对象EF

这是我的视图代码

public partial class MainWindow : Window
        {
            int[] Cod_Prod = new int[] { 1298, 1299, 1300, 1301, 1302, 1302, 1303, 1304, 1305 };

    public MainWindow()
    {
        InitializeComponent();
        GetCooks();
    }

    RosendaEntities db = new RosendaEntities();

    public ObservableCollection<ProcessingProduct> DataSource { get; set; }

    public void UpdateDataSource()
    {
        lbCooks.ItemsSource = DataSource;
    }

    public void GetCooks()
    {
        var s = new ObservableCollection<ProcessingProduct>();
        foreach (int c in Cod_Prod)
        {
            s.Add(new ProcessingProduct
            {
                CookingProduct = db.FullList.FirstOrDefault(x => x.Code == c),
                Ready = db.Art_Cook.Any(x => x.Cod_FullList == c && x.Cooks.Estate == (int)CooksState.Ready) ? (int)db.Art_Cook.Where(x => x.Cod_FullList == c && x.Cooks.Estate == (int)CookEstate.Ready).Sum(x => x.Quantity) : 0,
                Next = new ObservableCollection<Cooks>(db.Cooks.Where(x => x.Estate == (int)CookEstate.ProlongedCook || x.Estate == (int)CookEstate.Cooking).ToList())
            });
        }
        DataSource = s;
        UpdateDataSource();
    }
}

从EF的Cooks对象创建我以后分配给ItemsSource的ProcessingProduct集合,然后我得到这个错误,考虑一下:

  • 如果你没有加载或不指定ProcessingProduct属性Next不抛出该异常,那么我认为这是NextAvailable ListBox抛出此异常
  • 我没有更改列表的任何元素,也没有使用属性ItemsControl.Items
  • 在ObservableCollection之前,类型变量是IEnumerable,但是读了好几个帖子建议使用ObservableCollection,但更改它甚至不是我的问题解决 知道它可能是什么或者起源在哪里? 任何想法都欢迎提前感谢

修改

我发现如果我删除

<ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>

来自NextAvailable ListBox它正确加载,有谁知道为什么? 我怎样才能将它们放在水平的Alignament中,除了这个例外?

0 个答案:

没有答案