简体   繁体   English

将HashTable绑定到WPF中的ListView

[英]Bind HashTable to ListView in WPF

I have a hash table and I want to bind it to a ListView in wpf in the code or code behind. 我有一个哈希表,我想将其绑定到wpf中的代码或后面的代码中的ListView上。

My ListView is 我的ListView是

  <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>

On the code beind I have no idea how to bind it, but before I was using this in a Windows Forms app 在代码beind上,我不知道如何绑定它,但是在Windows Forms应用程序中使用它之前

        //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);

    //}

But now it doesnt work properly please help 但现在它不能正常工作,请帮助

You're just missing the standard binding. 您只是缺少标准绑定。 Here's the XAML for a ListBox: 这是ListBox的XAML:

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

And here's the XAML for a DataGrid: 这是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>

In your view model (or code-behind), you need to have your source data: 在视图模型(或隐藏代码)中,您需要具有源数据:

    public Collection<Application> Applications
    {
        get { return this._applications; }

        private set
        {
            this._applications = value;
            this.NotifyPropertyChanged(() => this.Applications);
        }
    }

And: 和:

    public Application SelectedApplication
    {
        get { return this._selectedApplication; }

        set
        {
            this._selectedApplication = value;
            this.NotifyPropertyChanged(() => this.SelectedApplication);
        }
    }

Just google or read about binding and you'll be all set. 只是谷歌或阅读有关绑定,你会万事俱备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM