如何让 WPF ListView 与列表正确绑定,以便视图在数据更改时更新?

发布于 2024-10-22 04:39:08 字数 1187 浏览 1 评论 0原文

迄今为止,我拥有的每个 ListView 我只是在 Xaml 中设置 ItemSource={Binding} ,然后在 .CS 文件中我说 listview.datacontext = myobject ,视图加载得很好。但现在我需要一个随着数据更新而更新的列表。因此,经过一些研究,我发现了 ObservableCollections 并重写了我的代码来使用它。但将列表视图设置为我的数据对象时,我无法显示数据。

我的 Xaml:

            <ListView ItemsSource="{Binding Tests}" Name="DataCompareTests" Margin="0,0,5,0" Grid.Column="0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="TestCase" Width="200" DisplayMemberBinding="{Binding name}" />
                    </GridView>
                </ListView.View>
            </ListView>

我的 Xaml.cs:

readonly DataCompare dataCompare = new DataCompare();

public void Execute_Click(object sender, RoutedEventArgs e)
    {

        var Tests = new ObservableCollection<TestCases>();
        Tests = dataCompare.LoadTestCases();  //located in another class file
        //DataCompareTests.DataContext = Tests;
    }

如果我删除 Xaml 中绑定的“测试”部分并删除上面 .DataContext 行中的注释,视图将显示正确的信息。然而,我的假设是,如果我希望视图随着数据而更新,我需要在绑定中指定我的对象。我该如何正确设置呢?我似乎找不到正确的答案。

谢谢,

杰森

To date every ListView I've had I just set ItemSource={Binding} in my Xaml and then in the .CS file I say listview.datacontext = myobject and the view loads just fine. But now I need to have a list that updates as the data updates as well. So after some research I discovered ObservableCollections and rewrote my code to use that. But I can't get my data to display when setting the listview to my dataobject.

My Xaml:

            <ListView ItemsSource="{Binding Tests}" Name="DataCompareTests" Margin="0,0,5,0" Grid.Column="0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="TestCase" Width="200" DisplayMemberBinding="{Binding name}" />
                    </GridView>
                </ListView.View>
            </ListView>

My Xaml.cs:

readonly DataCompare dataCompare = new DataCompare();

public void Execute_Click(object sender, RoutedEventArgs e)
    {

        var Tests = new ObservableCollection<TestCases>();
        Tests = dataCompare.LoadTestCases();  //located in another class file
        //DataCompareTests.DataContext = Tests;
    }

If I remove the "Tests" part of the binding in my Xaml and remove the comments from the .DataContext line above, the view displays the correct information. However it's my assumption that if I want my view to update as the data does I need to specify my object in the binding. How do I properly set that? I can't seem to find the correct answer.

Thanks,

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-10-29 04:39:08

我认为您需要更好地熟悉绑定和面向对象编程。

如果将数据上下文设置为模型对象,则“.Tests”应该是该模型对象的公共属性。另外,不要这样做:

var someVariable = new SomeClassThatTakesWorkToConstruct();
someVarialbe = someOtherVariable.SomeMethod();

你想做的是:
var someVariable = someOtherVariable.SomeMethod();

这是有两个很好的理由:1)你没有浪费 ObservableCollection 的构造。 2)您的代码将更容易重构(SomeMethod 返回的类型可以更改,而无需更改 someVariable 的声明)。

编辑,其他资源:

数据绑定概述
您已指定路径,但没有指定绑定的

MVVM 文章
关于使用常见 MVVM WPF 模式的精彩文章,即使在复杂的 UI 交互中,也可以帮助您保持代码面向对象、干净等。

I think you need to familiarize yourself a little better with bindings and object oriented programming in general.

If you set your datacontext to your model object, ".Tests" should be a public property of that model object. Also, don't do this:

var someVariable = new SomeClassThatTakesWorkToConstruct();
someVarialbe = someOtherVariable.SomeMethod();

What you meant to do was this:
var someVariable = someOtherVariable.SomeMethod();

This is for 2 good reasons 1) You are not wasting the construction of an ObservableCollection. 2) Your code will be easier to refactor (the type returned by SomeMethod can change without you having to alter your declaration of someVariable).

Edit, additional resources:

Databinding Overview
You've got a path specified but no source for the binding specified.

MVVM Article
Great article on using the common MVVM WPF pattern, helps you keep your code object oriented, clean, etc. even with complex UI interaction.

坐在坟头思考人生 2024-10-29 04:39:08

看来我的担心是毫无意义的,我一开始就是以正确的方式做这件事的。

根据 MSDN:

“但是,如果要绑定到已创建的对象,则需要在代码中设置 > DataContext,如以下示例所示。
...
myListBox.DataContext = myDataSet;"

我的对象已经创建,并且我确实在代码中设置了 DataContext。我所要做的就是将 ListView ItemSource 保留为 {Binding},每次添加到 Tests 对象时,列表都会更新..

我不敢相信我花了一整天的时间怀疑我是否正确地执行了此操作,而没有继续检查:-)。

It would appear my concerns were pointless and I WAS doing this the proper way in the first place.

According to MSDN:

"However, if you are binding to an object that has already been created, you need to set > the DataContext in code, as in the following example.
...
myListBox.DataContext = myDataSet;"

My object was already created, and I did set the DataContext in the code. All I had to do was leave the ListView ItemSource as {Binding} and each time I added to the Tests object, the list updated..

I can't believe I spent an entire day doubting I was doing this correctly without moving forward to check. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文