【问题标题】:Reference ViewModel's DataContext from within a DataTemplate in a DataTemplate从 DataTemplate 中的 DataTemplate 中引用 ViewModel DataContext
【发布时间】:2013-08-16 03:18:12
【问题描述】:

我发现自己陷入了某种束缚……有了束缚……呵呵……(蹩脚)

无论如何...我需要参考主 ViewModel 的属性,但在 DataTemplate 内,它本身在另一个 DataTemplate... 看起来 Silverlight 允许的唯一 RelativeSource 模式是: 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>

【问题讨论】:

    标签: silverlight windows-phone-7 xaml mvvm windows-phone-8


    【解决方案1】:

    如果您不想解决或轻松解决问题, 有事可做;

    使用静态资源

    看看这个;

       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 中添加数据上下文。类似&lt;UserControl.Resources&gt;&lt;local:MyViewModelClass x:Key="MainVieModel"/&gt;&lt;UserControl.Resources&gt;
    • 请注意我在代码//We use samve VM instace... 中的评论,这样做并不容易导致 xaml 呈现顺序,实例创建顺序请查看 stackoverflow.com/q/10171630/413032 在代码隐藏中执行现在似乎更好。
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2014-07-30
    • 2016-01-19
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多