我已经开始将我的项目从其当前不稳定的性质转变为更高效的 MVVM 模型,因为这样做,我在视图区域中创建的 ListBoxes 不再呈现。代码构建和运行顺利,但列表框控件保持为空.. 一旦我从不同的类调用方法 fillList 就会发生这种情况
public void FillList(List<StockItem> sList)
{
List<StockItem> locallist = new List<StockItem>();
locallist.Add(new StockItem { AvailableStock = " ijeifji", BlockedStock = "ijji", DetailedInfo = "rrkorkork", IDCode = "rokro", TotalStock = " iijerijer", UOM = "o", WarehouseName = "blargh" });
locallist.Add(new StockItem { AvailableStock = " ijeifji", BlockedStock = "ijji", DetailedInfo = "rrkorkork", IDCode = "rokro", TotalStock = " iijerijer", UOM = "o", WarehouseName = "blargh" });
locallist.Add(new StockItem { AvailableStock = " ijeifji", BlockedStock = "ijji", DetailedInfo = "rrkorkork", IDCode = "rokro", TotalStock = " iijerijer", UOM = "o", WarehouseName = "blargh" });
listBox.ItemsSource = locallist;
// listBox.ItemsSource = siList;
}
在这里,我创建了一个名为 locallist 的 TEMPORAL 列表来离线测试我的连接,发送到该方法的列表 sList 是一个类似的列表,包含具有相同绑定名称的项目,只是方式......更大......
我的列表框的 XAML 是:
<ListBox x:Name="listBox" FontSize="26" SelectionChanged="listBox_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="176" Width="444" Background="#00292828">
<Grid Height="159" VerticalAlignment="Top" Background="#6C5A5A5A">
<TextBlock Text="{Binding IDCode}" Height="28" Margin="8,7,164,0" VerticalAlignment="Top" Foreground="White" FontFamily="Segoe WP Light" FontSize="17.333"/>
<TextBlock Text="{Binding DetailedInfo}" Margin="8,39,164,0" Height="28" VerticalAlignment="Top" FontFamily="Segoe WP Light" FontSize="17.333"/>
<TextBlock Margin="24,73,15,0" TextWrapping="Wrap" FontFamily="Segoe WP Black" Height="29" VerticalAlignment="Top"><Run Text="TOTAL "/><Run Text=" AVAILABLE "/><Run Text=" BLOCKED"/></TextBlock>
<TextBox HorizontalAlignment="Left" Margin="8,0,0,-4" Text="{Binding TotalStock}" VerticalAlignment="Bottom" Height="61" Width="97" FontSize="18.667" Style="{StaticResource TextBoxStyle1}" Foreground="White" FontFamily="Segoe WP SemiLight" TextAlignment="Center"/>
<TextBox Margin="144,0,0,-4" Text="{Binding AvailableStock}" VerticalAlignment="Bottom" Height="61" FontSize="18.667" Style="{StaticResource TextBoxStyle1}" Foreground="White" FontFamily="Segoe WP SemiLight" TextAlignment="Center" HorizontalAlignment="Left" Width="102"/>
<TextBox HorizontalAlignment="Right" Margin="0,0,40,-4" Text="{Binding BlockedStock}" VerticalAlignment="Bottom" Height="61" Width="108" FontSize="18.667" Style="{StaticResource TextBoxStyle1}" Foreground="White" FontFamily="Segoe WP SemiLight" TextAlignment="Center"/>
<TextBlock HorizontalAlignment="Left" Margin="111,0,0,6.733" TextWrapping="Wrap" Text="{Binding UOM}" VerticalAlignment="Bottom" FontFamily="Segoe WP Light" FontSize="13.333"/>
<TextBlock HorizontalAlignment="Right" Margin="0,0,172.343,6.733" TextWrapping="Wrap" Text="{Binding UOM}" VerticalAlignment="Bottom" FontFamily="Segoe WP Light" FontSize="13.333"/>
<TextBlock HorizontalAlignment="Right" Margin="0,0,15.343,6.733" TextWrapping="Wrap" Text="{Binding UOM}" VerticalAlignment="Bottom" FontFamily="Segoe WP Light" FontSize="13.333"/>
</Grid>
<Button Content="Details" HorizontalAlignment="Right" VerticalAlignment="Top" Height="73" Width="146" Style="{StaticResource ButtonStyle1}" Foreground="#FFAAC028"/>
<Rectangle Fill="White" Margin="0,-0.167,150.589,0" StrokeThickness="3" StrokeLineJoin="Round" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" HorizontalAlignment="Right" Height="57.75" VerticalAlignment="Top" Width="6.906">
<Rectangle.RenderTransform>
<CompositeTransform SkewX="21.737" TranslateX="19.935"/>
</Rectangle.RenderTransform>
</Rectangle>
<!--<Button Click="Button_Click"/>-->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当我从 ViewModel.cs 调用 FillList 方法(位于 StockItemOverview.xaml.cs 中)时,问题就开始了(这里列表被创建并发送到 FillList(List sList) 一旦程序完成 FillList 方法,它继续运行,但没有出现列表框!请帮助!
从我的 ViewModel.cs 编辑代码
public void PopulateListbox(List<StockItem> sentSI, int MAX)
{
List<StockItem> stockItemList = new List<StockItem>();
for (int i = 0; i < MAX; i++)
{
stockItemList.Add(new StockItem() { IDCode = sentSI[i].IDCode, DetailedInfo = sentSI[i].DetailedInfo, AvailableStock = sentSI[i].AvailableStock, BlockedStock = sentSI[i].BlockedStock, TotalStock = sentSI[i].TotalStock, UOM = sentSI[i].UOM });
//stockItemList.Add(new StockItem() { IDCode = "Hubla", DetailedInfo = "17 C", AvailableStock = "20", BlockedStock = "10", TotalStock = "30" });
}
StockItemOverview pageDisplayer = new StockItemOverview();
pageDisplayer.FillList(stockItemList);
}
和 StockItem 类:
public class StockItem
{
public string IDCode { get; set; }
public string AvailableStock { get; set; }
public string BlockedStock { get; set; }
public string TotalStock { get; set; }
public string DetailedInfo { get; set; }
public string WarehouseName { get; set; }
public string UOM { get; set; }
}