2

Link the observable collection

 2 years ago
source link: https://www.codesd.com/item/link-the-observable-collection.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Link the observable collection

advertisements

How would I go about binding this in 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. The collection is getting added too but the ListView won't populate.

I haven't been using WPF long so any insight would be great.

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:

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

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.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK