views:

32

answers:

1

I have an object in my business layer (let's call it Car for example's sake), and I have created a UserControl (CarIcon). The user control has a property that is a Car object and when the Car object is set it calls a method that configures the various properties of the user control.

Now I am rewriting it to use dependency properties so that I can use data templates and bind the backing Car object to the CarIcon user control at run time. Ideally, I want to bind the object to the user control and have the user control automatically update it's display.

It seems like the dependency property is not being set and the app is display blank icons (because without the backing object there is no data to display). Am I binding things incorrectly?

Also Is this a reasonable approach (considering I don't want to implement a full MVVM for a simple control)?

Binding the Car to the CarIcon in xaml

<ItemsControl Name="m_itemsControl" Focusable="false"
    pixellabs:AnimatingTilePanel.ItemHeight="100" pixellabs:AnimatingTilePanel.ItemWidth="300">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <pixellabs:AnimatingTilePanel Width="300" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <sample:CarIcon CurrentCar="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Code for the dependency property

    public Car CurrentCar {
        get { return (Car)GetValue( CarProperty ); }
        set { SetValue( CarProperty, value ); }
    }

    public static readonly DependencyProperty CarProperty =
        DependencyProperty.Register( 
            "CurrentCar", 
            typeof( Car ), 
            typeof( CarIcon ), 
            new UIPropertyMetadata() );
A: 

Change the first argument to name of property (CurrentCar instead of Car). You should also provide some default value, for example null or new Car()

public static readonly DependencyProperty CarProperty = 
        DependencyProperty.Register(  
            "CurrentCar",  
            typeof(Car),  
            typeof(CarIcon),  
            new UIPropertyMetadata(new Car())); 
Wallstreet Programmer
Ok, fixed that but I am still not getting the binding. The ObservableCollection I am binding items source to works, and I get four blank icons showing up, but the individual bindings in each data template seem to not work. Also, if I initialize new Car() in the DP.Register, does that make it static? To make it non-static you have to initialize it in the cctor I thought.
vfilby
Please create another question for your problems with the data template. The default value is used for the CurrentCar property until it is changed by some other code calling CurrentCar set property or through data binding.
Wallstreet Programmer