0

Windows Phone 7.1 project (WP 8.0 SDK), I want to pass current item in ItemTemplate to a user control.

XAML:

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:ShipControl Ship="{Binding}"  x:Name="ShipControl"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

Code behind ShipControl:

public object Ship 
    {
        get
        {
            return GetValue(ShipProperty);
        }
        set
        {
            SetValue(ShipProperty, value);
        }
    }

    //Used by xaml binding
    public static readonly DependencyProperty ShipProperty = DependencyProperty.Register("Ship", typeof(Ship), typeof(Ship), new PropertyMetadata(null, new PropertyChangedCallback(OnShipChanged)));

    private static void OnShipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //TODO: Set break point here

        return;
    }

However, when debugging Ship an object of value DataBinding is passed as value, not a Ship (therefore return type is object instead of Ship). That eventually causes an exception on SetValue. Other bindings on Ship-properties do work, so I really have no idea. According to this question, above should work:

WPF Pass current item from list to usercontrol

See here for sample project which throws exception on data binding, because passed object is Binding instead of data object. http://dl.dropbox.com/u/33603251/TestBindingApp.zip

Community
  • 1
  • 1
Boland
  • 155
  • 1
  • 14

2 Answers2

0

You need to put a x:Name="MyControl" in your control, and then your binding will look like Ship="{Binding ElementName=MyList, Path=CurrentItem}" instead of just {Binding} (which does not mean much AFAIK). Your control needs to expose the CurrentItem property.

If you do not want to explicity name your control, you can try to play with Relative Source but I did not try myself so cannot help you on this one.

Community
  • 1
  • 1
Qortex
  • 7,087
  • 3
  • 42
  • 59
  • {Binding} was what I read at some other sites, like the link I quoted: http://stackoverflow.com/questions/3106190/wpf-pass-current-item-from-list-to-usercontrol I will give it a try later on, thanks. – Boland Mar 11 '13 at 09:26
  • Please explain this further. I have to add a property in code behind, but what should it return?? I am in the ItemTemplate of the ItemsControl where the data-item is available, so I don't understand why I have to add code-behind. I just want to get the current data item. In ASP.Net this would be easy, why is this so hard in WPF / SL... – Boland Mar 11 '13 at 20:43
0

Your Dependency Property is badly formed so the XAML parser does not treat it as such.

You need to change your instance property type to Ship, and DependencyProperty owner type to ShipControl. Then the Binding will work (assuming that you are binding to a list of Ships).

public Ship Ship
{
    get { return (Ship)GetValue(ShipProperty); }
    set { SetValue(ShipProperty, value); }
}

public static readonly DependencyProperty ShipProperty =
    DependencyProperty.Register("Ship", typeof(Ship), typeof(ShipControl), new PropertyMetadata(null, new PropertyChangedCallback(OnShipChanged)));

private static void OnShipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    //TODO: Set break point here

    return;
}
Silver Solver
  • 2,310
  • 1
  • 13
  • 19
  • Does not work, tried that in the sample project. I get a COM-exception, because the passed object to Ship is not of type Ship. If I change it to object, I can see "value" in Ship_set is of type Binding instead of type Ship. – Boland Mar 11 '13 at 20:44
  • Sounds like the DP did work and your XAML binding is incorrect. Can you post the collection you are binding the ItemsSource of your ItemsControl to? – Silver Solver Mar 12 '13 at 07:16