将HashTable绑定到WPF中的ListView

时间:2011-12-06 14:14:13

标签: c# wpf listview hashtable

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

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

在代码beind上我不知道如何绑定它,但在我在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);

    //}

但现在它无法正常工作请帮助

1 个答案:

答案 0 :(得分:1)

您只是错过了标准绑定。这是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);
        }
    }

只需谷歌或阅读有关绑定的信息,您就可以了。