markv12
markv12

Reputation: 344

A sensible way to use data binding for a large software project.

I am looking into the logistics of adding several WPF features to a 1,000,000+ line code-base. I am looking into using databinding and DataTemplates in XAML to easily display various objects. I am able to bind an observable collection: points to a ListBox and if I don't use a template, I can just provided a meaningful toString for the list items and that gets me somewhere. The problem comes in when I want to use a template to display various properties of the list items. It is my understanding that they all have to be DependencyProperties. parent is just a property. It looks like adding dependency properties is fairly complicated. All the classes you want to display have to inherit from DependencyObject and you have to set a bunch of stuff for each property you want to use. This does not seem feasible for a large codebase, to have to do all this for each property. I'm wondering how databinding is used in practice, do you really add crap-tons of dependency properties to all of your classes or is there a better way?

Thanks!

    <ListBox Name="listBox1" 
             ItemsSource="{Binding Source={StaticResource points}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="Point!"/>
                    <TextBlock Text="{Binding parent}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Upvotes: 1

Views: 98

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564423

The problem comes in when I want to use a template to display various properties of the list items. It is my understanding that they all have to be DependencyProperties. parent is just a property. It looks like adding dependency properties is fairly complicated. All the classes you want to display have to inherit from DependencyObject and you have to set a bunch of stuff for each property you want to use.

This is not true.

You need to use a Dependency Property or have your type implement INotifyPropertyChanged, but this is only required if you want the binding to work bi-directionally. This requirement exists so that WPF gets notified when a property value changes, and the UI can update automatically. However, if the property value isn't changing while the screen is displayed, there is no requirement (other than the value be a Property, and not a Field).

If you're just displaying the values within the object, and they won't change while being displayed, standard properties will work fine.

Upvotes: 2

Related Questions