如何显示/隐藏 DataTemplates UWP
How to show / hide DataTemplates UWP
我正在忙于 UWP 应用程序。我有一个网格视图,其中包含数据模板项。设置如下:
<GridView ItemsSource="{x:Bind location}">
<GridView.ItemTemplate>
<DataTemplate x:Name="columnTemplate" x:DataType="data:Locations">
<StackPanel x:Name="columnStack">
<Image Width="100" Source="{x:Bind locationColumnImage}"/>
<TextBlock FontSize ="16" Text ="{x:Bind numberOfBales}"
HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
它是从我应用程序的 Models 文件夹中的 class 绑定的。
class 看起来像这样;
public class 位置管理器
{
public List<Locations> getLocations()
{
//represents db value of how many items in a particlar location
var baleCol = buildColumnList();
var location = new List<Locations>();
location.Add(new Locations { locationColumn = "A", locationColumnImage = "Assets/A.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("A")).Count() });
location.Add(new Locations { locationColumn = "B", locationColumnImage = "Assets/B.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("B")).Count() });
location.Add(new Locations { locationColumn = "C", locationColumnImage = "Assets/c.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("C")).Count() });
location.Add(new Locations { locationColumn = "D", locationColumnImage = "Assets/D.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("D")).Count() });
location.Add(new Locations { locationColumn = "E", locationColumnImage = "Assets/E.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("E")).Count() });
location.Add(new Locations { locationColumn = "F", locationColumnImage = "Assets/F.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("F")).Count() });
location.Add(new Locations { locationColumn = "G", locationColumnImage = "Assets/G.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("G")).Count() });
return Locations;
}
}
}
它基本上是在一个网格中显示一个字母(a 到 G)的图像,然后在该字母下方显示一个数字(取决于有多少/如果有任何字母在其位置 A、B、C 中存储了一个项目。 ..)
我想知道的是,如果我不想显示图像下方带有 0 的项目(如果 G 中没有存储任何内容,则不显示它)我该怎么做?
在您的 return 语句中而不是
return location;
改为
return location.Where(a => a.numberOfBales > 0);
这样您将只会收到计数 > 0 的项目,并且只会显示那些项目。
我正在忙于 UWP 应用程序。我有一个网格视图,其中包含数据模板项。设置如下:
<GridView ItemsSource="{x:Bind location}">
<GridView.ItemTemplate>
<DataTemplate x:Name="columnTemplate" x:DataType="data:Locations">
<StackPanel x:Name="columnStack">
<Image Width="100" Source="{x:Bind locationColumnImage}"/>
<TextBlock FontSize ="16" Text ="{x:Bind numberOfBales}"
HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
它是从我应用程序的 Models 文件夹中的 class 绑定的。 class 看起来像这样; public class 位置管理器
{
public List<Locations> getLocations()
{
//represents db value of how many items in a particlar location
var baleCol = buildColumnList();
var location = new List<Locations>();
location.Add(new Locations { locationColumn = "A", locationColumnImage = "Assets/A.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("A")).Count() });
location.Add(new Locations { locationColumn = "B", locationColumnImage = "Assets/B.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("B")).Count() });
location.Add(new Locations { locationColumn = "C", locationColumnImage = "Assets/c.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("C")).Count() });
location.Add(new Locations { locationColumn = "D", locationColumnImage = "Assets/D.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("D")).Count() });
location.Add(new Locations { locationColumn = "E", locationColumnImage = "Assets/E.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("E")).Count() });
location.Add(new Locations { locationColumn = "F", locationColumnImage = "Assets/F.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("F")).Count() });
location.Add(new Locations { locationColumn = "G", locationColumnImage = "Assets/G.jpg", numberOfBales = baleCol.Where(a => a != null && a.StartsWith("G")).Count() });
return Locations;
}
}
}
它基本上是在一个网格中显示一个字母(a 到 G)的图像,然后在该字母下方显示一个数字(取决于有多少/如果有任何字母在其位置 A、B、C 中存储了一个项目。 ..)
我想知道的是,如果我不想显示图像下方带有 0 的项目(如果 G 中没有存储任何内容,则不显示它)我该怎么做?
在您的 return 语句中而不是
return location;
改为
return location.Where(a => a.numberOfBales > 0);
这样您将只会收到计数 > 0 的项目,并且只会显示那些项目。