C#
WPF
我有一个包含 3 列的列表视图。我想要发生的是,当我双击一行时,它将捕获所选行的列的值并将它们存储为变量,以便我可以将它们传递到另一个页面或根据需要简单地操作它们。我这里有我的练习代码。现在,我只是在填充一个消息框,看看我是否可以正确地取出变量。正在发生的事情是它没有从行中抓取任何东西。我通过删除if
块来解决这个问题,发现它string name = selectedObject.ilName;
是空的。我还有一个问题是关于ingListLV.SelectedItems[0] as InventoryList
[0] 的声明,它到底指的是什么?我最初认为它指的是它返回的值,所以 [0] 将是第 1 列中的值,[1] 将是第 2 列等,但我知道这是错误的。
这是我的XAML
<ListView x:Name="ingListLV" HorizontalAlignment="Center" Height="100" Margin="0,145,0,0" VerticalAlignment="Top" Width="Auto"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
SelectedValuePath="InventoryName"
Style="{DynamicResource listViewStyle}"
MouseDoubleClick="Open_Edit">
<ListView.View>
<GridView>
<GridViewColumn x:Name="InvName" Width="100" Header="Name" DisplayMemberBinding="{Binding Path=InventoryName}" />
<GridViewColumn Width="50" Header="Qty" DisplayMemberBinding="{Binding Path=Qty}" />
<GridViewColumn Width="50" Header="Type" DisplayMemberBinding="{Binding Path=Type}" />
</GridView>
</ListView.View>
</ListView>
和我背后的代码
private void Open_Edit(object sender, RoutedEventArgs e)
{
var selectedObject = ingListLV.SelectedItems[0] as InventoryList;
if (selectedObject == null)
{
return;
}
string name = selectedObject.ilName;
MessageBox.Show(name);
}
public class InventoryList
{
public string ilName { get; set; }
public decimal ilQty { get; set; }
public string ilType { get; set; }
}
编辑 这是我将数据加载到列表视图的地方
private void LoadLV()
{
auroraDataEntities = new AuroraDataEntities();
ObjectQuery<Inventory> inventories = auroraDataEntities.Inventories;
//Returns only opjects with a quantity greater than 0, so it won't show anything you are out of
var fillList = from q in inventories
where q.Qty > 0
select q;
ingListLV.ItemsSource = fillList.ToList();
}