Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 参考视图模型';从DataTemplate中的DataTemplate中删除DataContext_Silverlight_Windows Phone 7_Xaml_Mvvm_Windows Phone 8 - Fatal编程技术网

Silverlight 参考视图模型';从DataTemplate中的DataTemplate中删除DataContext

Silverlight 参考视图模型';从DataTemplate中的DataTemplate中删除DataContext,silverlight,windows-phone-7,xaml,mvvm,windows-phone-8,Silverlight,Windows Phone 7,Xaml,Mvvm,Windows Phone 8,我发现自己有点左右为难。。。带绑定的。。。呵呵。。。(跛脚) 无论如何。。。我需要引用主ViewModel的属性,但是从一个数据模板中引用,它本身在另一个数据模板中。。。Silverlight允许的相对资源模式似乎只有:Self和TemplatedParent。而且TemplatedParent还不够高 有什么可以做的吗 一些代码: <phone:PanoramaItem> <phone:LongListSelector Margin="0,-38,-22,2" Ite

我发现自己有点左右为难。。。带绑定的。。。呵呵。。。(跛脚)

无论如何。。。我需要引用主ViewModel的属性,但是从一个
数据模板
中引用,它本身在另一个
数据模板
中。。。Silverlight允许的
相对资源
模式似乎只有:Self和TemplatedParent。而且TemplatedParent还不够高

有什么可以做的吗

一些代码:

<phone:PanoramaItem>
    <phone:LongListSelector Margin="0,-38,-22,2" ItemsSource="{Binding Items}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <common:ItemContentTemplateSelector DataContext="{Binding}" Content="{Binding ItemContent}" HorizontalContentAlignment="Stretch" Margin="12,2,0,4">
                    <common:ItemContentTemplateSelector.DefaultTemplate>
                        <DataTemplate>
                            .....
                        </DataTemplate>
                    </common:ItemContentTemplateSelector.DefaultTemplate>
                    <common:ItemContentTemplateSelector.PhoneNumberTemplate>
                        <DataTemplate>
                            <Grid Background="White" Height="102">
                                <Border x:Name="border">
                                    <TextBlock Text="Call"/>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseLeftButtonDown">
                                     <!-- BINDING ERROR -->
                                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.PhoneCallCommand}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </common:ItemContentTemplateSelector.PhoneNumberTemplate>
                </common:ItemContentTemplateSelector>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</phone:PanoramaItem>

.....

如果您不想找到解决方法或使其变得容易, 有事情要做

使用静态资源

看看这个,

   MyViewModelClass ViewModel
   {
        get;set;
   }

  ...ctor()
  {
     this.ViewModel=new MyViewModelClass();
     this.DataContext=this.ViewModel; //We use same VM instance as DataContext as Resource
     this.Resoureces.Add("MainVieModel",ViewModel);
     InitializeComponents();//...Add resource before this line
  }
之后,您可以将ViewModel作为静态资源同时使用到任何您想要的地方

{Binding Path=Items,Source={StaticResource ViewModel}}

我在stackoverflow的编辑器上编码。可能缺少一些字符…

解决方法很好!谢谢是否可以仅在XAML中添加此静态资源?我已经使用此解决方案多年了。您的意思是在XAML端添加此资源吗?是的,你可以这样做。您还可以在xaml中添加datacontext。类似这样的内容请注意我在代码中的注释
//我们使用samve VM instance…
这样做并不容易,因为xaml呈现顺序、实例创建顺序都很复杂,现在在codebehind中这样做似乎更好。