WP7:绑定到外面pivot.itemtemplate元素 [英] WP7: Binding to an element outside pivot.itemtemplate

查看:100
本文介绍了WP7:绑定到外面pivot.itemtemplate元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力查找有关此一段时间。我有点新手,但我潜伏了很多,还是没能找到解决我的问题,所以我决定在这里发表我的问题。

I've been struggling for a while on this. I'm a bit of a newbie, but i lurked a lot and still couldn't find a solution to my problem, so I decided to post my question here.

我一个支点结合我要显示创建一个画廊对象的集合。这里是我的支点控制绑定到一个名为影集列表,其中每个对象都包含两个字符串(URL和说明)。

I'm binding a pivot to a collection of objects I want to display to create a gallery. Here is my pivot control bound to a list called gallery, where each object contains 2 strings (url and description).

<controls:Pivot ItemsSource="{Binding gallery}" Grid.Row="0" x:Name="galleryPivot">
            <controls:Pivot.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Image Source="{Binding url}" />
                            <Grid Visibility="{Binding ElementName=galleryPivot, Path=DataContext.ShowDetail}">
                                <ListBox>
                                    <ListBoxItem>
                                        <StackPanel>
                                            <TextBlock Text="{Binding description}" />
                                        </StackPanel>
                                    </ListBoxItem>
                                </ListBox>
                            </Grid>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </controls:Pivot.ItemTemplate>
        </controls:Pivot>

DataContext的是视图模型,并在页面的构造初始化。
这是我的ViewModel:

The datacontext is the viewmodel and initialized in the constructor of the page. Here is my ViewModel:

public class GalleryViewModel : INotifyPropertyChanged
{
    public List<Gallery> gallery
    {
        get { return Globals.pictures; }

    }

    private Visibility _showDetail = Visibility.Collapsed;
    public Visibility ShowDetail
    {
        get { return _showDetail; }
        set {
           _showDetail = value;
           RaisePropertyChanged("ShowDetail");
        }
    }       
    public GalleryViewModel()
    { }

    public event PropertyChangedEventHandler PropertyChanged = delegate { return; };
    protected void RaisePropertyChanged(string propertyName)
    {
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

画廊目的是在我的视图模型的列表,作为ShowDetail属性。由于ShowDetail是范围之内,我试图设置的ElementName作为解释<一个href=\"http://stackoverflow.com/questions/4640370/wp7-why-does-a-listbox-itemspanel-break-my-elementname-data-binding\">here.

枢轴结合井画廊名单,但是当我改变ShowDetail的值,网格将无处藏身。我也试过的ElementName设置为LayoutRoot但它仍然是行不通的。

The pivot binds well to the gallery list, but when I change the value of ShowDetail, the grid won't hide. I also tried to set ElementName to LayoutRoot but it still won't work.

我的问题,我怎么可以绑定能见度当它是ItemTemplate中的范围之内?

My question, how can I bind the visibility when it is outside the scope of the itemtemplate?

推荐答案

在一个的DataTemplate 的ElementName 结合仅指元素的名称是内的DataTemplate 。数据模板中的数据上下文是图库实例,而不是 GalleryViewModel 。你可以移动的 ShowDetail 属性下降到图库类代替。

Within a DataTemplate the ElementName binding refers only to the names of elements that are within that DataTemplate. The data context within your data template is the Gallery instance, not the GalleryViewModel. You could move the ShowDetail property down to the Gallery class instead.

如果你不想这样做,那么一个替代方法是使用代理服务器的数据上下文,您作为资源添加到页面并绑定到页面的数据上下文( GalleryViewModel 实例presumably)。然后,您可以参考这个资源,你会任何其他资源在父数据背景下得到的。

If you'd rather not do that, then an alternative would be to use a proxy for the data context, which you add as a resource to the page and bind to the page's data context (a GalleryViewModel instance presumably). You can then reference that resource as you would any other resource to get at the parent data context.

如果你不熟悉这个代理的概念,那么<一个href=\"http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx\"相对=nofollow>丹Wahlin对这个问题的后应该有所帮助。

If you're not familiar with this proxy concept, then Dan Wahlin's post on the subject should help.

这篇关于WP7:绑定到外面pivot.itemtemplate元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆