无法绑定ListView C#

时间:2014-04-26 17:15:39

标签: c# wpf listview

无法使用ListView绑定ObservableCollection。在ListView中不显示任何内容。请告诉我我做错了什么。

我不知道还有什么要写,但论坛不允许我创建一个主题代码块

C#代码:

public ObservableCollection<MySQLCommand> _queryInf = new ObservableCollection<MySQLCommand>();
....
public ObservableCollection<MySQLCommand> queryInf
{
    get { return _queryInf; }
}
....
if (buffer[4] == 3)
{
     Dispatcher.Invoke(new Action(() =>
     _queryInf.Add(new MySQLCommand() { Number = 0, Command = Encoding.ASCII.GetString(buffer, 5, byteRec - 4), Memory = 0, Rows = 0, Time = 0 })));

}
...
public class MySQLCommand
{
    public int Number { get; set; }
    public string Command { get; set; }
    public int Time { get; set; }
    public int Memory { get; set; }
    public int Rows { get; set; }
}

XAML代码:

<ListView HorizontalAlignment="Left" Height="313" Margin="224,66,0,0" VerticalAlignment="Top" Width="752" x:FieldModifier="public" Name="tProfiler" ItemsSource="{Binding queryInf}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="номер" DisplayMemberBinding="{Binding Number}"></GridViewColumn>
                    <GridViewColumn Header="запрос" DisplayMemberBinding="{Binding Command}"></GridViewColumn>
                    <GridViewColumn Header="время мс" DisplayMemberBinding="{Binding Time}"></GridViewColumn>
                    <GridViewColumn Header="принято кб" DisplayMemberBinding="{Binding Memory}"></GridViewColumn>
                    <GridViewColumn Header="строки" DisplayMemberBinding="{Binding Rows}"></GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

2 个答案:

答案 0 :(得分:0)

您的绑定语法很好。你能从你的视图模型中绑定任何东西吗?正如@stukselbax所提到的,在输出栏中加注星标是一个好主意。

答案 1 :(得分:0)

如果您在代码中添加tProfiler.ItemsSource = _queryInf;而不是在XAML中,则数据将显示在ListView中