0

我已经开始将我的项目从其当前不稳定的性质转变为更高效的 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; }
}
4

2 回答 2

0

只是一些想法。

  1. 创建一个 BaseModel 类。
  2. 一个。实施 INotifyPropertyChanged(使用适当的命名空间)。湾。实施方法。

例子:

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
   PropertyChangedEventHandler handler = PropertyChanged;
   if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
}
  1. 您的所有模型类(例如 StockItem)都将从 BaseModel 类继承。

  2. 为 StockItem 类的所有字段详细说明 get 和 set。并通过在 Set 方法中将相应的公共属性名称作为参数来调用 PropertyChanged 方法。

例子:

private string _idCode;

public string IDCode
{
 get{
      return _idCode;
    }
 set{
     _idCode = value;
     PropertyChanged("IDCode");
    }
}
  1. 将所有 List<> 转换为 ObservableCollection<>

  2. 让您的 ViewModel 实现 INotifyPropertyChanged 接口或从 BaseViewModel 继承(如上面解释的 BaseModel,它实现了该接口)。定义集合也是一个属性。像我们对 Model 类所做的那样调用 PropertyChanged 方法。

  3. 有 listBox.ItemsSource = locallist; Load 事件或构造函数中的语句。

  4. 将 DataContext 属性设置为 ViewModel。

于 2012-04-24T14:06:21.833 回答
0

因此,在另一天搞砸代码之后,我注意到我创建了 2 个 ViewModel 实例并调用了彼此的 StockList ...通过正确使用相同的 ViewModel 实例,列表框现在可以正确绘制,你必须更改StockItem 类:

public class StockItem
{
        // Insert code required on object creation below this point.
        //      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; }

        //public StockItem() { }

        // public StockItem(string idcode, string avstock, string blstock, string totstock, string detinfo, string warehouse, string uom) 
        //  {
        //         this.IDCode = idcode;
        //         this.AvailableStock = avstock;
        //         this.BlockedStock = blstock;
        //         this.TotalStock = totstock;
        //         this.DetailedInfo = detinfo;
        //         this.WarehouseName = warehouse;
        //         this.UOM = uom;

        //     }
    private string _idcode;

    public string IDCode
    {
        get
        {
            return _idcode;
        }
        set
        {
            if (value != _idcode)
            {
                _idcode = value;
                NotifyPropertyChanged("IDCode");
            }
        }
    }
    private string _avstock;

    public string AvailableStock
    {
        get
        {
            return _avstock;
        }
        set
        {
            if (value != _avstock)
            {
                _avstock = value;
                NotifyPropertyChanged("AvailableStock");
            }
        }
    }
    private string _blstock;

    public string BlockedStock
    {
        get
        {
            return _blstock;
        }
        set
        {
            if (value != _blstock)
            {
                _blstock = value;
                NotifyPropertyChanged("BlockedStock");
            }
        }
    }
    private string _totstock;

    public string TotalStock
    {
        get
        {
            return _totstock;
        }
        set
        {
            if (value != _totstock)
            {
                _totstock = value;
                NotifyPropertyChanged("TotalStock");
            }
        }
    }
    private string _detinf;

    public string DetailedInfo
    {
        get
        {
            return _detinf;
        }
        set
        {
            if (value != _detinf)
            {
                _detinf = value;
                NotifyPropertyChanged("DetailedInfo");
            }
        }
    }
    private string _uom;

    public string UOM
    {
        get
        {
            return _uom;
        }
        set
        {
            if (value != _uom)
            {
                _uom = value;
                NotifyPropertyChanged("UOM");
            }
        }
    }
    private string _wareh;

    public string WarehouseName
    {
        get
        {
            return _wareh;
        }
        set
        {
            if (value != _wareh)
            {
                _wareh = value;
                NotifyPropertyChanged("WarehouseName");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

并将 ItemsSource 的绑定添加到正确的视图模型

private void StockItemOverview_Loaded(object sender, RoutedEventArgs e)
    {
        this.listBox.ItemsSource = App.ViewModel.StockItemList;

        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }
于 2012-04-25T11:01:34.907 回答