简体   繁体   English

绑定可观察的集合

[英]Bind Observable collection

How would I go about binding this in XAML? 我将如何在XAML中进行绑定?

I am looking to use an observable collection to populate a ListView ever time a method is called which will be through a drop event on another control. 我希望在ListView调用方法时都使用可观察的集合来填充ListView ,该方法将通过另一个控件上的drop事件。 The collection is getting added too but the ListView won't populate. 该集合也被添加,但是ListView不会填充。

I haven't been using WPF long so any insight would be great. 我没有使用WPF很久了,所以任何见识都将是很棒的。

namespaceA 
{
    public class SomeClassA
    {
        public string FirstName { get; set; }
    }

    public class SomeClassB
    {
        public void MethodA()
        {
            ObservableCollection<SomeClassA> Name_Col = new ObservableCollection<SomeClassA>();
            Name_col.Add(new SomeClassA { FirstName = "SomeValue" });
        }
    }
}

XAML: XAML:

 <ObjectDataProvider
    x:Key="Viewmodel"
    ObjectType="{x:Type Local:NamespaceA}"/>

 <ListView DataContext="{StaticResource Viewmodel}"
    Height="396" 
    HorizontalAlignment="Left" 
    Margin="766,67,0,0" 
    Name="listView1" 
    VerticalAlignment="Top" 
    Width="260" 
    ItemsSource="{Binding Name_col}" />

The only things you can access in bindings are public ( internal doesn't work) properties and indexers of public classes. 绑定中唯一可以访问的是publicinternal不起作用)属性和public类的索引器。

And no matter how, variables declared inside a method will never be accessible from anywhere outside the said method. 而且无论如何, 在方法内部声明的变量将永远无法从该方法外部访问。 It's a common mistake people who used to work with scripting languages often make. 这是过去使用脚本语言的人们经常犯的错误。 To access something outside a method (or inside of another), that must be declared outside of the method. 要访问某个方法外部(或另一个内部)的内容,必须在该方法外部声明该内容。

One useful hint: Visual Studio's Output window is a very useful tool for tracking binding errors. 一个有用的提示:Visual Studio的“ Output窗口是跟踪绑定错误的非常有用的工具。

On a side note: Bindings are case sensitive. 附带说明:绑定区分大小写。 Even if your code followed the rules mentioned above, WPF would still not find the binding source, as your binding path is Name_col , but the property's name is Name_Col . 即使您的代码遵循上述规则,WPF仍将找不到绑定源,因为您的绑定路径是Name_col ,但是属性的名称是Name_Col

There are quite a few things wrong with your code 您的代码有很多错误

First off, your DataContext is pointing to a namespace, not an object. 首先,您的DataContext指向名称空间,而不是对象。 Change that to be an instance of an object. 将其更改为对象的实例。

<ObjectDataProvider x:Key="Viewmodel" ObjectType="{x:Type local:SomeClassB}"/>

Or 要么

<local:SomeClassB x:Key="Viewmodel" />

Second, your ObservableCollection is not a public property, so your View cannot see nor access it. 其次,您的ObservableCollection不是公共属性,因此您的View无法看到或访问它。

public class SomeClassB
{
    public ObservableCollection<SomeClassA> Name_Col { get; set; }

    public void MethodA()
    {
        Name_Col = new ObservableCollection<SomeClassA>();
        Name_col.Add(new SomeClassA { FirstName = "SomeValue" });
    }
}

And last of all, WPF bindings are case-sensitive so you need to fix the ItemsSource binding to use the correct case 最后,WPF绑定区分大小写,因此您需要修复ItemsSource绑定以使用正确的大小写

<ListView ... 
    ItemsSource="{Binding Name_Col}" />

Assuming this code is pasted, your binding is looking at a nonexistent property. 假设粘贴了此代码,则您的绑定正在查看不存在的属性。 Try capitalizing the 'c' in your binding. 尝试在绑定中将“ c”大写。 Name_col -> Name_Col Name_col-> Name_Col

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

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