列表框控件隐藏,不显示
本文关键字:显示 隐藏 控件 列表 | 更新日期: 2023-09-27 18:32:02
我已经开始将我的项目从当前的不稳定性质转变为更高效的MVVM模型,这样做后,在我的视图区域中创建的 ListBox 不再呈现。代码构建并运行平稳,但是列表框控件保持为空..一旦我从不同的类调用方法 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调用FillList方法(位于StockItemOverview.xaml.cs中)时,问题就开始了.cs(这里列表被创建并发送到FillList(List sList)一旦程序完成 FillList 方法,它就会继续运行,但不显示列表框!请帮忙!
编辑我的视图模型中的代码.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);
}
和库存项类:
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; }
}
只是一些想法。
- 创建一个基本模型类。
- a. 实现 INotifyPropertyChanged (使用适当的命名空间)。b.实现这些方法。
例:
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
所有模型类(例如 StockItem)都将继承自 BaseModel 类。
为 StockItem 类的所有字段拼写获取和设置。并通过在 Set 方法中将相应的公共属性名称作为参数来调用 PropertyChanged 方法。
例:
private string _idCode;
public string IDCode
{
get{
return _idCode;
}
set{
_idCode = value;
PropertyChanged("IDCode");
}
}
将所有列表<>转换为可观察集合<>
让你的 ViewModel 实现 INotifyPropertyChanged 接口或从 BaseViewModel 继承(如上面解释的实现接口的 BaseModel)。将集合定义为属性。调用 PropertyChanged 方法,就像我们对 Model 类所做的那样。
在 Load 事件或构造函数中具有 listBox.ItemsSource = locallist; 语句。
将 DataContext 属性设置为 ViewModel。
所以在另一天搞砸了代码之后,我注意到我创建了 ViewModel 的 2 个实例并调用彼此的库存列表......通过正确使用视图模型的同一实例,列表框现在可以正确绘制,您必须将 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));
}
}
}
并将项目源的绑定添加到正确的视图模型
private void StockItemOverview_Loaded(object sender, RoutedEventArgs e)
{
this.listBox.ItemsSource = App.ViewModel.StockItemList;
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}