开发者

Silverlight4 DataTemplates

I have a ItemsControl with its ItemsSource bound to a collection, additionally I have a ItemTemplateSelector set.

It works well however the Da开发者_C百科taTemplateSelector only allows me to access the items bound by ItemsSource, however I want to use the Parents DataContext to make the decision on what item template should be used.

Is this achievable in SL4??

If so how can it be achieved??


Thanks for your response @Xin but I managed to resolve my problem by doing the following.

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        DataTemplate dt = null;

        switch ((DataContext as PlanViewModel).Plan.Status)
        {
            case Infrastructure.Services.Web.PlanStatus.Appraisal:
                dt = (DataTemplate)this.Resources["Appraisal"];
                break;
            case Infrastructure.Services.Web.PlanStatus.Maintenance:
                dt = (DataTemplate)this.Resources["Maintenance"];
                break;
            case Infrastructure.Services.Web.PlanStatus.Setting:
                dt = (DataTemplate)this.Resources["Setting"];
                break;
        }

        itemsControl1.ItemTemplate = dt;
    }


Yes it is.

Name your layout root 'LayoutRoot', then you can do

<TextBlock Text="{Binding DataContext.SomeTextInParent, ElementName=LayoutRoot}" />

in your item template.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜