绑定到ItemsControl中的当前项

时间:2013-03-03 19:49:35

标签: wpf windows-phone-7 windows-phone-8

Windows Phone 7.1项目(WP 8.0 SDK),我想将ItemTemplate中的当前项传递给用户控件。

XAML:

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

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;
    }

但是,在调试Ship时,值为DataBinding的对象作为值传递,而不是Ship(因此返回类型是对象而不是Ship)。这最终会导致SetValue出现异常。 Ship-properties上的其他绑定确实有效,所以我真的不知道。根据这个问题,上面应该有效:

WPF Pass current item from list to usercontrol

任何??

0 个答案:

没有答案