开发者

Create composite DataContext in XAML for a Usercontrol

I am trying to create a composite DataContext for a UserControl. Basically I have a control which has Order and Package properties and I wanted to create the composite object representing this datasource in XAML rather than in code.

This is how I am trying to display the UserControl (and create the DataContext):

<views:PackageDetailsControl>
    <views:PackageDetailsControl.DataContext>
        <vm:OrderPackagePair Package="{Binding Package, Mode=OneWay}" 
                             Order="{Binding Order, Mode=OneWay}"/>                 
    </views:PackageDetailsControl.DataContext>
</views:PackageDetailsControl>  

The OrderPackagePair object is a simple dependency object that is created in XAML :

pu开发者_如何学JAVAblic class OrderPackagePair : DependencyObject
{
    public OrderDetails Order
    {
        get { return (OrderDetails)GetValue(OrderProperty); }
        set { SetValue(OrderProperty, value); }
    }

    public static readonly DependencyProperty OrderProperty =
        DependencyProperty.Register("Order", typeof(OrderDetails), typeof(OrderPackagePair), new UIPropertyMetadata(null));

    public PackageInfo Package
    {
        get { return (PackageInfo)GetValue(PackageProperty); }
        set { SetValue(PackageProperty, value); }
    }

    public static readonly DependencyProperty PackageProperty =
        DependencyProperty.Register("Package", typeof(PackageInfo), typeof(OrderPackagePair), new UIPropertyMetadata(null));
}

Order and Package are not bound correctly and are just null.

Yes I know there's probably a better way of doing this - but I cannot understand why this isn't working. Occasionally in Blend it'll work and then go blank again.


This will not work because DependencyObject(OrderPackagePair class) doesn't monitor internal changes of its dependency properties. As OrderPackagePair object remains the same, DataContext considered as unchanged.

On the opposite site, class Freezable is intented to notify subscribers that instance was changed when one of its dependency properties changed.

So, try to declare Freezable instead of DependencyObject as base class of OrderPackagePair.

------------- UPDATE --------

Yes, it works. In order to prove it I've implemented simple example.

Code of OrderPackagePairClass:

public class OrderPackagePair : Freezable
{
    public OrderDetails Order
    {
        get { return (OrderDetails)GetValue(OrderProperty); }
        set { SetValue(OrderProperty, value); }
    }

    public static readonly DependencyProperty OrderProperty =
        DependencyProperty.Register("Order", typeof(OrderDetails), typeof(OrderPackagePair), new UIPropertyMetadata(null));

    public PackageInfo Package
    {
        get { return (PackageInfo)GetValue(PackageProperty); }
        set { SetValue(PackageProperty, value); }
    }

    public static readonly DependencyProperty PackageProperty =
        DependencyProperty.Register("Package", typeof(PackageInfo), typeof(OrderPackagePair), new UIPropertyMetadata(null));

    protected override Freezable CreateInstanceCore()
    {
        throw new NotImplementedException();
    }
}

XAML:

<Window x:Class="WindowTest.MainWindow"
        xmlns:self="clr-namespace:WindowTest"
        Name="RootControl">
    <StackPanel Margin="10" DataContextChanged="StackPanel_DataContextChanged">
        <StackPanel.DataContext>
            <self:OrderPackagePair Package="{Binding Path=DataContext.PackageInfo, Mode=OneWay, ElementName=RootControl}" 
                                   Order="{Binding Path=DataContext.OrderDetails, Mode=OneWay, ElementName=RootControl}"/>
        </StackPanel.DataContext>

        <Button Margin="10" Content="Change Package" Click="Button_Click"/>
    </StackPanel>
</Window> 

And code behind:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private OrderDetails _orderDetails;
    public OrderDetails OrderDetails
    {
        get
        {
            return this._orderDetails;
        }
        set
        {
            this._orderDetails = value;
            this.OnPropertyChanged("OrderDetails");
        }
    }

    private PackageInfo _packageInfo;
    public PackageInfo PackageInfo
    {
        get
        {
            return this._packageInfo;
        }
        set
        {
            this._packageInfo = value;
            this.OnPropertyChanged("PackageInfo");
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.PackageInfo = new PackageInfo(DateTime.Now.ToString());
    }

    private void StackPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        Trace.WriteLine("StackPanel.DataContext changed");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        var safeEvent = this.PropertyChanged;
        if (safeEvent != null)
        {
            safeEvent(this, new PropertyChangedEventArgs(name));
        }
    }
}

When you click the button, model changes PackageInfo property (for simplicity model and view are implemented in the same class). Dependency property OrderPackagePair.Package reacts on new value and overwrites its value. Due to Freezable nature, OrderPackagePair notifies all subscribers that it was changed and handler StackPanel_DataContextChanged is called. If you get back to DependencyObject as base class of OrderPackagePair - handler will be never called.

So, I suppose your code doesn't work because of other mistakes. You should carefully work with DataContext. For example, you wrote:

<views:PackageDetailsControl>
    <views:PackageDetailsControl.DataContext>
        <vm:OrderPackagePair Package="{Binding Package, Mode=OneWay}" 
                             Order="{Binding Order, Mode=OneWay}"/>                 
    </views:PackageDetailsControl.DataContext>
</views:PackageDetailsControl>

and certainly this is one of the problems. Binding expression is oriented on current DataContext. But you set DataContext as OrderPackagePair instance. So you binded OrderPackagePair.Package to OrderPackagePair.Package (I suppose, that your goal is to bind OrderPackagePair.Package to Model.Package). And that's why nothing happened.

In my example in binding expression I explicitly tell to which DataContext I want to bind:

 Package="{Binding Path=DataContext.PackageInfo, Mode=OneWay, ElementName=RootControl}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜