在WPF中将HashTable绑定到ListView

本文关键字:ListView 绑定 HashTable WPF 中将 | 更新日期: 2023-09-27 18:16:51

我有一个哈希表,我想将它绑定到wpf中的ListView代码或后面的代码。

My ListView is

  <ListView Canvas.Left="1045" Canvas.Top="634" Height="244" Name="lvContact" Width="536" >
            <ListView.View>
                <GridView x:Name="gvContacts">
                    <GridView.Columns>
                        <GridViewColumn Width="200" x:Name="ContactName" DisplayMemberBinding="{Binding Path=Username}"></GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

在后面的代码我不知道如何绑定它,但在我使用这个在Windows窗体应用程序

        //foreach (DictionaryEntry de in contactList)
        //{
        //    ListViewItem contactItem = new ListViewItem(de.Key.ToString());
        //    if (de.Value.ToString() == "NLN")
        //    {
        //        contactItem.ForeColor = System.Drawing.Color.Green;
        //    }
        //    else
        //    {
        //        contactItem.ForeColor = System.Drawing.Color.Gray;
        //    }

        //    lvContact.Items.Add(contactItem);
    //}

但是现在它不能正常工作,请帮助

在WPF中将HashTable绑定到ListView

您只是错过了标准绑定。下面是ListBox的XAML:

        <ListBox DockPanel.Dock="Bottom" ItemsSource="{Binding Applications}" DisplayMemberPath="Name"
                 SelectedItem="{Binding SelectedApplication}" Height="auto"/>

下面是DataGrid的XAML:

        <DataGrid Height="280" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column"
              ItemsSource="{Binding SelectedApplication.Tasks}"
              SelectedItem="{Binding SelectedTask}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Sequence}" Header="Order" Width="50" />
                <DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" Width="*"/>
                <DataGridTextColumn Binding="{Binding Path=TaskType}" Header="Type" Width="120"/>
                <DataGridTextColumn Binding="{Binding Path=FailureCausesAllStop}" Header="Stop" Width="50"/>
            </DataGrid.Columns>
        </DataGrid>

在视图模型(或代码隐藏)中,需要有源数据:

    public Collection<Application> Applications
    {
        get { return this._applications; }
        private set
        {
            this._applications = value;
            this.NotifyPropertyChanged(() => this.Applications);
        }
    }

:

    public Application SelectedApplication
    {
        get { return this._selectedApplication; }
        set
        {
            this._selectedApplication = value;
            this.NotifyPropertyChanged(() => this.SelectedApplication);
        }
    }

只要谷歌一下或阅读一下有关绑定的知识,你就会万事通了。