【问题标题】:C# how to use ListView selecteditemC#如何使用ListView selecteditem
【发布时间】:2018-07-03 09:42:42
【问题描述】:

我对 C# 和 WPF 非常陌生。我已经开始了新项目来学习如何一起使用它们以及如何构建 UI。 Bearly 开始我一直坚持使用 listview 集合中的 selecteditem。我试图显示从所选对象中获取的一些基本信息。在表单上我添加了标签和一些按钮。主要目标是打开第二个表格,其中包含有关所选记录的详细信息。但首先我想实现一些简单的东西 - 只是在标签控件中显示记录 ID。我可以获取记录并用记录填充列表框,但是所有尝试读取选定数据的尝试都失败了(标签中没有显示任何内容)。你能帮我看看如何在标签场景中使用 selecteditem 吗?并希望给我一些关于详细信息窗口场景的建议...... 无论如何 - 所有关于我的代码的 cmets 都将不胜感激,请耐心等待,记住这是我第一次处理这个问题。

为方便起见,BitBucket 提供了所有代码:https://bitbucket.org/is-smok/gama

感谢您的帮助。

MainWindow.xaml 文件的一部分

    <Grid>
    <ListView x:Name="lstInventory" Height="180" Margin="5,51,79,0" VerticalAlignment="Top" 
              ItemsSource="{Binding GetInventory}"  
              SelectedItem="{Binding SelectedInventory, Mode=TwoWay}" 
              DisplayMemberPath="Inventory_id">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="GamaID" DisplayMemberBinding="{Binding Inventory_id}" />
                <GridViewColumn Header="Typ"  DisplayMemberBinding="{Binding Serial_number}" />
                <GridViewColumn Header="Producent"  DisplayMemberBinding="{Binding Registry_number}" />
            </GridView>
        </ListView.View>
    </ListView>
    <Button Content="Add" HorizontalAlignment="Left" Margin="57,265,0,0" VerticalAlignment="Top" Width="75"   Click="AddInventory_Click"/>
    <Button Content="Remove" HorizontalAlignment="Left" Margin="137,265,0,0" VerticalAlignment="Top" Width="75"   Click="RemoveInventory_Click"/>
    <Button Content="Edit" HorizontalAlignment="Left" Margin="217,265,0,0" VerticalAlignment="Top" Width="75"  Click="EditInventory_Click"/>
    <Label Content="{Binding SelectedInventory.Serial_number}" HorizontalAlignment="Left" Margin="120,326,0,0" VerticalAlignment="Top" Height="24" Width="140"/>
    <Label x:Name="lblInventoryId" Content="{Binding SelectedInventory.Inventory_id}" HorizontalAlignment="Left" Margin="10,326,0,0" VerticalAlignment="Top" Height="24" Width="105"/>
</Grid>

MainWindow.xaml.cs 的一部分

    public MainWindow()
    {
        InitializeComponent();

        DataAccess dataAccess = new DataAccess();
        inventory = dataAccess.GetIventory();
        lstInventory.ItemsSource = inventory;
        lstInventory.DisplayMemberPath = "inventory_id";
    }

DataAccess.cs 文件的一部分

    private Inventory m_SelectedInventory;
    public Inventory SelectedInventory
    {
        get
        {
            return m_SelectedInventory;
        }
        set
        {
            m_SelectedInventory = value;
        }
    }

【问题讨论】:

  • 你应该先阅读Data Binding Overview。这里有一些错误或缺失的地方。 1. 将dataAccess对象分配给Window的DataContext:DataContext = dataAccess;。 2.将GetInventory方法变成属性Inventory,像ItemsSource="{Binding Inventory}"一样绑定,去掉lstInventory.ItemsSource = inventory。 3. 使DataAccess实现INotifyPropertyChanged接口并从SelectedInventory setter触发PropertyChanged事件。
  • @Clemens 谢谢你的回复。 Inventory GetInventory 方法和 Inventory 对象用于填充 ListView。这是我项目中唯一有效的部分。但我会尝试遵循您的建议并使其发挥作用。但首先要阅读建议的文档。泰。

标签: c# wpf listview selecteditem


【解决方案1】:

您能帮我看看如何在标签场景中使用 selecteditem 吗?

您可以直接绑定到ListViewSelectedItem 属性:

<Label x:Name="lblInventoryId" Content="{Binding SelectedItem.Inventory_id, ElementName=lstInventory}" ... />

但是要能够绑定到您的SelectedInventory 属性,您应该做的是将窗口的DataContext 设置为DataAccess 对象:

public MainWindow()
{
    InitializeComponent();

    DataContext = new DataAccess();
}

如果您在 XAML 中绑定的 GetInventoryDataAccess 类的公共 属性,则绑定应该可以工作:

ItemsSource="{Binding GetInventory}"  

您不能绑定到方法。所以你应该在DataAccess 类中调用GetIventory() 方法并通过属性公开结果,例如:

public DataAccess
{
    public DataAccess()
    {
        Inventories = GetIventory();
    }

    public IEnumerable Inventories { get; private set; }

   //...
}

XAML:

ItemsSource="{Binding Inventories}"

还要注意DataAccess 应该实现INotifyPropertyChanged 接口,并在每次设置SelectedInventory 属性时向UI 发出通知。请参考MSDN了解更多信息。

【讨论】:

  • 嗨。很抱歉回复晚了,我的工作很艰难,我不得不专注于其他一些项目,所以我让这个暂时搁置了。现在,有一些空闲时间,我回到了它,并根据您的好建议进行了一些更改。一切都按预期工作。仍在努力获得预期的结果,在我的情况下是在新的应用程序窗口中打开详细的库存信息,但现在你的所有建议都完美无缺。如果您对将记录传递到新窗口有任何其他提示,我将不胜感激。
【解决方案2】:

您好,许多问题之一是标签仅在初始化时获取值。 SelectedInventory 已正确更新,但标签无法识别它。要实现这一点,请阅读INotifyPropertyChanged 接口,了解更多信息here

接口的实现:

public event PropertyChangedEventHandler PropertyChanged;
public Inventory SelectedInventory
{
    get
    {
        return m_SelectedInventory;
    }
    set
    {
        m_SelectedInventory = value;
        RaisePropertyChanged(nameof(SelectedInventory));
    }
}

private void RaisePropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

【讨论】:

  • 可以,但根据他的问题,这应该可以解决问题
  • 不,请阅读我对这个问题的评论。一个好的答案应该解决所有这些问题。现在,{Binding SelectedInventory} 根本不工作。
  • 这个答案很有帮助。使用来自此威胁的答案和 cmets,我设法修复了我的代码。谢谢。
【解决方案3】:

ListView 的 ItemsSource 必须绑定到 IEnumerable 或派生类型的 property,wpf 不支持绑定到方法。

【讨论】:

  • 有人诅咒我,因为每次我发布答案我都会立即得到-1。呵呵
猜你喜欢
  • 2017-11-08
  • 1970-01-01
  • 2011-01-18
  • 2011-02-06
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2018-09-03
相关资源
最近更新 更多