你好,我想知道在XAML中是否可以绑定到一个列表,其中一个值具有特定的值。
例如,在下面的示例中,是否可能仅使用XAML显示Price = 20的项目?
我之所以问这个问题,是因为我要绑定包含另一个列表的对象列表,我只想显示某些项目,具体取决于它们的值。因此,我试图避免使用C#解决方案。
MainWindow.xaml
<Window x:Class="Binding_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding}"/>
</Grid>
</Window>
MainWindow.xaml.cs
public MainWindow()
{
DataContext = BuildList();
InitializeComponent();
}
List<Product> BuildList()
{
var list = new List<Product>();
var y = 1;
for (var i = 0; i < 100; i++)
{
list.Add(new Product{Name = string.Format("Item {0}",i), Price = y++ * 10 });
if (y > 3)
y = 1;
}
return list;
}
Product.cs
public class Product
{
public string Name { get; set; }
public int Price { get; set; }
public override string ToString()
{
return string.Format("{0} \t{1}", Name, Price.ToString("C2"));
}
}