这是我的情况.
viewmodelA {  
    ObservableCollection<Items> ItemsA  
    ObservableCollection<viewmodelB> viewmodelBs
}

使用设置为viewmodel A的datacontext查看A.
ViewA具有包含列表框,面板,文本块等的全景图,其中列表框的项目源绑定到ItemsA

我想在运行时使用commond数据模板(列表框,文本块等)将其他全景项添加到全景控件中.每个全景项将在运行时绑定到viewmodelBs集合中的每个viewmodelB.

我并不反对为此做一些代码隐藏的东西,因为我不是一个严格的mvvm纯粹主义者.
但如果我可以指定控制和数据模板并使其工作,那么解决方案可能会很优雅.我是wpf / xaml的新手,并试图通过编写一个wp7应用程序来使用mvvm光框架来破解这些技术..最后,我希望我动态生成的全景项目/列表框内容触发中继命令viewmodel他们被绑定…

这是一些我试过工作失败的代码片段.希望它提供一些想法..

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="PanoramaItemStyle" targettype="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate targettype="ContentControl">
                    <Grid x:Name="ContentGrid">
                        <controls:PanoramaItem x:Name="ItemLocationPanoramaItem" Header="{Binding TagName}">
                            <StackPanel >
                                <ListBox  x:Name="ItemLocatorsList" ItemsSource="{Binding ItemLocators}" Height="496" SelectedItem="{Binding SelectedItemLocation,Mode=TwoWay}" >
                                    <Custom:Interaction.Triggers>
                                        <Custom:EventTrigger EventName="SelectionChanged">
                                            <galaSoft_MvvmLight_Command:EventToCommand x:Name="SelectionChangedEvent" Command="{Binding RelativeSource={RelativeSource TemplatedParent},Path=DataContext.GoToEditItemLocatorCommand}" PassEventArgsToCommand="True"/>
                                        </Custom:EventTrigger>
                                    </Custom:Interaction.Triggers>
                                    <ListBox.ItemsPanel>
                                        <ItemsPanelTemplate >
                                            <StackPanel Orientation="Vertical"  ScrollViewer.VerticalScrollBarVisibility="Auto" />
                                        </ItemsPanelTemplate>
                                    </ListBox.ItemsPanel>
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel> 
                                                    <StackPanel Orientation="Horizontal" Margin="0,17">
                                                        <StackPanel Width="311">
                                                            <TextBlock Text="{Binding Path=Item.Name}" textwrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}"/>
                                                            <TextBlock Text="{Binding Path=Location.Description}" textwrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                    </StackPanel> 
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </controls:PanoramaItem>
                        <ContentPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Foreground" Value="White"/>
    </Style> 
</phone:PhoneApplicationPage.Resources>































代码隐藏:
private LocationGroupsviewmodel viewmodel = null;

public LocationGroups()
    {
        InitializeComponent(); 
        LocationGroupsPanaroma.DefaultItem = LocationGroupsPanaroma.Items[0];
         viewmodel = this.DataContext as LocationGroupsviewmodel;
         CreateDynamicPanaromaItems();
    }


    private void CreateDynamicPanaromaItems()
    {
        foreach (Model.LocationGroup group in viewmodel.LocationGroups)
        {
            if (group.TotalItems > 0)
            {
                PanoramaItem pi = new PanoramaItem();
                pi.Header = group.Name;
                pi.Orientation = System.Windows.Controls.Orientation.Horizontal;
                ItemLocationListviewmodel itemLocationviewmodel = viewmodel[group.LocationGroupId];
                pi.DataContext = itemLocationviewmodel;
                pi.Style = Resources["PanoramaItemStyle"] as Style;
                LocationGroupsPanaroma.Items.Add(pi);

            }
        }

    }

编辑

viewmodel A有

Items collection

Collection of viewmodelBs

panaroma数据上下文设置为viewmodelA

panaroma item - Statitically created in xaml to some Items collection in viewmodelA

    This pan item has a list Box



panaroma items --- to be bound to collection of viewmodelbs

    These pan items should each have a listBox which is selectable
                  and  bound to some collection in View Model B and fires commands on    selection         changed to viewmodelB. Currently using the galasoft eventtocommand to hook the selection changed on the
    list Box to a relay command. The problem is that this eventtommand should have the viewmodel as its data context and the not the collection (bound to the listBox) within viewmodel.
好的,终于有时间回答这个问题了.提出的解决方案不需要任何代码,仅依赖于MVVM概念和数据绑定.

概念上,Panorama控件是一个ItemPresenter(它继承自ItemsPresenter),即您可以将ItemsSource绑定到包含重复您的PanoramaItems的项目的列表.

要渲染PanoramaItem,您必须提供Panorama.HeaderTemplate和Panorama.ItemTemplate的模板.模板内的DataContext是代表PanoramaItem的viewmodel.如果此viewmodel包含项目列表,您现在可以使用它来生成您正在寻找的ListBoxes.

以下是样本……

viewmodelLocator.cs

using galaSoft.MvvmLight;

namespace WP7Test.viewmodel
{
    public class viewmodelLocator
    {
        private static Mainviewmodel _main;

        public viewmodelLocator()
        {
    if (viewmodelBase.IsInDesignModeStatic) {
        // Create design time services and viewmodels
    } else {
        // Create run time services and view models
    }
            _main = new Mainviewmodel();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance","CA1822:MarkMembersAsstatic",Justification = "This non-static member is needed for data binding purposes.")]
        public Mainviewmodel Main
        {
            get
            {
                return _main;
            }
        }
    }
}

Mainviewmodel.cs

public class Mainviewmodel : viewmodelBase
{
    public Mainviewmodel()
    {
        this.Items = new ObservableCollection<Itemviewmodel>();

        if (IsInDesignMode) {
            // Code runs in Blend --> create design time data.
        } else {
            // Code runs "for real"
        }
        this.LoadData();
    }

    #region [Items]

    public const string ItemsPropertyName = "Items";

    private ObservableCollection<Itemviewmodel> _items = default(ObservableCollection<Itemviewmodel>);

    public ObservableCollection<Itemviewmodel> Items {
        get {
            return _items;
        }
        private set {
            if (_items == value) {
                return;
            }

            var oldValue = _items;
            _items = value;

            RaisePropertyChanged(ItemsPropertyName);
        }
    }

    #endregion

    private void LoadData() {
        this.Items.Add(new Itemviewmodel() { LineOne = "runtime one",LineTwo = "Maecenas praesent accumsan bibendum",LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" });
        this.Items.Add(new Itemviewmodel() { LineOne = "runtime two",LineTwo = "Dictumst eleifend facilisi faucibus",LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus" });
        this.Items.Add(new Itemviewmodel() { LineOne = "runtime three",LineTwo = "Habitant inceptos interdum lobortis",LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent" });

        foreach (var item in Items) {
            for (int i = 0; i < 5; ++i)
                item.Items.Add(new Itemviewmodel() { LineOne = "Item " + i,LineTwo = "Maecenas praesent accumsan bibendum" });
        }
    }
}

Itemviewmodel.cs

public class Itemviewmodel : viewmodelBase
{
    public Itemviewmodel() {
        this.Items = new ObservableCollection<Itemviewmodel>();

        if (IsInDesignMode) {
            // Code runs in Blend --> create design time data.
        } else {
            // Code runs "for real": Connect to service,etc...
        }
    }

    public override void Cleanup() {
        // Clean own resources if needed

        base.Cleanup();
    }

    #region [LineOne]

    public const string LineOnePropertyName = "LineOne";

    private string _lineOne = default(string);

    public string LineOne {
        get {
            return _lineOne;
        }

        set {
            if (_lineOne == value) {
                return;
            }

            var oldValue = _lineOne;
            _lineOne = value;
            RaisePropertyChanged(LineOnePropertyName);
        }
    }

    #endregion

    #region [LineTwo]

    public const string LineTwoPropertyName = "LineTwo";

    private string _lineTwo = default(string);

    public string LineTwo {
        get {
            return _lineTwo;
        }

        set {
            if (_lineTwo == value) {
                return;
            }

            var oldValue = _lineTwo;
            _lineTwo = value;

            RaisePropertyChanged(LineTwoPropertyName);
        }
    }

    #endregion

    #region [LineThree]

    public const string LineThreePropertyName = "LineThree";

    private string _lineThree = default(string);

    public string LineThree {
        get {
            return _lineThree;
        }

        set {
            if (_lineThree == value) {
                return;
            }

            var oldValue = _lineThree;
            _lineThree = value;
            RaisePropertyChanged(LineThreePropertyName);
        }
    }

    #endregion

    #region [Items]

    public const string ItemsPropertyName = "Items";

    private ObservableCollection<Itemviewmodel> _items = default(ObservableCollection<Itemviewmodel>);

    public ObservableCollection<Itemviewmodel> Items {
        get {
            return _items;
        }
        private set {
            if (_items == value) {
                return;
            }

            var oldValue = _items;
            _items = value;
            RaisePropertyChanged(ItemsPropertyName);
        }
    }

    #endregion
}

MainPage.xaml中

<phone:PhoneApplicationPage 
    x:Class="WP7Test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800" 
    FontFamily="{StaticResource PhoneFontFamilynormal}"
    FontSize="{StaticResource PhoneFontSizenormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="False">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Main,Source={StaticResource Locator}}">
        <controls:Panorama Title="my application" ItemsSource="{Binding Items}">
            <controls:Panorama.Background>
                <ImageBrush ImageSource="PanoramaBackground.png"/>
            </controls:Panorama.Background>
            <controls:Panorama.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding LineOne}"/>
                </DataTemplate>
            </controls:Panorama.HeaderTemplate>
            <controls:Panorama.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Border BorderThickness="0,1" BorderBrush="White">
                            <TextBlock Text="{Binding LineTwo}" FontSize="28" textwrapping="Wrap"/>
                        </Border>
                        <Border BorderThickness="0,1" Margin="0,20" BorderBrush="White">
                            <TextBlock Text="{Binding LineThree}" textwrapping="Wrap"/>
                        </Border>
                        <ListBox ItemsSource="{Binding Items}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding LineOne}" FontSize="24"/>
                                        <TextBlock Text="{Binding LineTwo}" FontSize="18" Margin="24,5"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </controls:Panorama.ItemTemplate>
        </controls:Panorama>
    </Grid>
    <!--Panorama-based applications should not show an ApplicationBar-->
</phone:PhoneApplicationPage>

编辑 – 添加其他第一个面板

最后,我明白了你想要实现的目标!但是,您仍然无需执行任何代码!你只需要一个模板……对于这个Blend确实可以帮到你,因为它可以让你为一个现有的控件提取模板……好的,这里是变化.

首先,我向Mainviewmodel添加了一个新属性来显示一些数据:

#region [MainPageProperty]

public const string MainPagePropertyPropertyName = "MainPageProperty";
private string _mainPageProperty = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu";

public string MainPageProperty {
    get {
        return _mainPageProperty;
    }
    set {
        if (_mainPageProperty == value) {
            return;
        }

        _mainPageProperty = value;
        RaisePropertyChanged(MainPagePropertyPropertyName);
    }
}

#endregion

然后我使用Blend获取Panorama控件的模板并将其插入控件:Panorama元素.

<controls:Panorama.Template>
    <ControlTemplate targettype="controls:Panorama">
        <Grid>
            <Grid.RowDeFinitions>
                <RowDeFinition Height="auto"/>
                <RowDeFinition Height="*"/>
            </Grid.RowDeFinitions>
            <controlsPrimitives:PanningBackgroundLayer x:Name="BackgroundLayer" HorizontalAlignment="Left" Grid.RowSpan="2">
                <Border x:Name="background" Background="{TemplateBinding Background}" CacheMode="BitmapCache"/>
            </controlsPrimitives:PanningBackgroundLayer>
            <controlsPrimitives:PanningTitleLayer x:Name="TitleLayer" CacheMode="BitmapCache" ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" FontSize="187" FontFamily="{StaticResource PhoneFontFamilyLight}" HorizontalAlignment="Left" Margin="10,-76,9" Grid.Row="0"/>
            <controlsPrimitives:PanningLayer x:Name="ItemsLayer" HorizontalAlignment="Left" Grid.Row="1">
                <StackPanel Orientation="Horizontal">
                    <controls:PanoramaItem Header="Main panel" Width="432">
                        <TextBlock Text="{Binding ElementName=LayoutRoot,Path=DataContext.MainPageProperty}" textwrapping="Wrap"/>
                    </controls:PanoramaItem>
                    <ItemsPresenter x:Name="items"/>
                </StackPanel>
            </controlsPrimitives:PanningLayer>
        </Grid>
    </ControlTemplate>
</controls:Panorama.Template>

这里有两个技巧,首先我插入一个StacPanel,允许在controlPrimitives下面有多个元素:PanningLayer,名称为ItemsPanel.在这个StackPanel中,我移动了ItemsPresenter并添加了另一个PanoramaItem.但有一件重要的事情是设置PanoramaItem的Width属性,否则面板将扩展到所需的房间.

另一个技巧是,为了访问DataContext,我必须在Binding中使用ElementName.

希望这能展示MVVM和模板的力量!

windows-phone-7 – 全景wp7 mvvm中的静态和动态全景项目的更多相关文章

  1. Swift教程17-淡化MVC,使用MVVM框架开发轻巧便于维护的iOS/android app

    MVVM是微软提出一种移动开发框架,旨在针对传统的MVC框架,解决传统的MVC框架的控制器的臃肿问题.M:Model模型,也就是数据模型;比如一条微博,对应的所有字段合成一条微博整体,这个整体就是ModelV:View视图,只用来显示的视图,如iOS的UIView,Cell;当然在iOS中Storyboard中,view总是和控制器关联,这并不是严格的view如果我们纯手写代码定义一个view那么

  2. Swift 2.0 下面向协议的MVVM架构实践

    本文由CocoaChina译者lynulzy翻译原文:Swift2.0:Protocol-OrientedMVVM自从令人兴奋的[《面向协议的编程方法》]在Swift的WWDC大会上发布以来。我已经在之前的博客中使用过MVVM架构,如果你想了解更多MVVM相关知识请参考[这里]。接下来我将讲解,如何添加面向协议。switchToggle.onTintColor=switchColorself.onSwitchToggleHandler=onSwitchToggleHandler虽然在这种情况下看起来并不是

  3. Swift教程17-淡化MVC,使用MVVM框架开发轻巧便于维护的iOS app

    overridefuncawakeFromNib(){super.awakeFromNib()}overridefuncsetSelected{super.setSelected}/***配置Cell的内容方法*/funcconfigCellWithStatusModel(model:StatusModel!){self.imageView2.setimageWithURL;background-color:inherit">placeholderImage:UIImage)self.textLabel2

  4. Swift开发黑科技:还在争论MVC和MVVM?现在你应该试试MVP!

    我下定决心重构自己的代码,下面步入正题,结合Swift开发大会的一些分享,让我们谈谈架构。现在数据工程里面的目录是这样的:模型代码:为了简单我都使用了String类型的数据,至于为什么要使用struct而不使用class,大家可以参考WWDC2015的414号视频,讲的非常清楚,我自己的项目中的数据模型已经全部转成struct了,我会在后面专门写博文讲解struct,这里就不赘述了。

  5. MVVM 不是那么好

    我觉得MVVM是一种反人类的设计模式,它使架构更加混乱而非清晰。MVVM命名很糟糕名称是很重要的。ViewModel这一名称则没有发挥任何作用。ViewModel的第一种含义是modelfortheview。MVVM引进太多职责命名不够具体,导致这个类的任务无休止地增长。MVVM不改变你的架构viewmodel并不能从根本上改变你的应用程序的架构。我能想到的MVVM模式最大的好处就是它把“下水道”从苹果自带的viewcontrooller类转移到了viewmodel这一自定义的对象。

  6. Swift学习第十一枪-基于协议的MVVM模式的实现

    下面是我的新建的Swift学习交流群,欢迎大家一起来共同学习Swift。不管是IOS还是Android,就三种常用模式,MVC,MVP,MVVM网上的资料非常之多,对于MVVM大家估计都有所了解,我在这里就简单的以图示的形式给大家展示。

  7. 关于MVVM的理解

    MVVM中VM到底是个什么角色?MVVM简介关于MVVM,相信大家或多或少都有了解。引用MVVM介绍文中一图受MVC或MVP架构的影响,对MVVM最初印象以为这是一个以viewmodel为核心,处理View和Model的开发架构。于是乎在原有MVC的基础上,创建了一个所谓的viewmodel对象,然后把ViewController中的代码移到viewmodel中,在viewmodel里面处理View以及Model的所有逻辑。完整结构如图所示Model并不表示ModelMVVM架构中的M,并不表示Model

  8. android – MVVM Dagger2与组件中存在匹配键的绑定

    我正在使用以下谷歌示例项目:https://github.com/googlesamples/android-architecture-components作为我的新项目的参考,并且难以尝试向项目添加第二个活动.这是编译时遇到的错误这是我的代码ActivityModuleAppComponentAppInjector的AppModuleFragmentBuildersModule注射viewmod

  9. vue MVVM双向绑定实例详解(数据劫持+发布者-订阅者模式)

    使用vue也好有一段时间了,也算对其双向绑定原理也有了解个大概,这篇文章主要给大家介绍了关于vue MVVM双向绑定(数据劫持+发布者-订阅者模式)的相关资料,需要的朋友可以参考下

  10. Android开发框架MVC-MVP-MVVM-MVI的演变Demo

    这篇文章主要为大家介绍了Android开发框架MVC-MVP-MVVM-MVI的演变Demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

随机推荐

  1. static – 在页面之间共享数据的最佳实践

    我想知道在UWP的页面之间发送像’selectedItem’等变量的最佳做法是什么?创建一个每个页面都知道的静态全局变量类是一个好主意吗?

  2. .net – 为Windows窗体控件提供百分比宽度/高度

    WindowsForm开发的新手,但在Web开发方面经验丰富.有没有办法为Windows窗体控件指定百分比宽度/高度,以便在用户调整窗口大小时扩展/缩小?当窗口调整大小时,可以编写代码来改变控件的宽度/高度,但我希望有更好的方法,比如在HTML/CSS中.在那儿?

  3. 使用Windows Azure查询表存储数据

    我需要使用特定帐户吗?>将应用程序部署到Azure服务后,如何查询数据?GoogleAppEngine有一个数据查看器/查询工具,Azure有类似的东西吗?>您可以看到的sqlExpressintance仅在开发结构中,并且一旦您表示没有等效,所以请小心使用它.>您可以尝试使用Linqpad查询表格.看看JamieThomson的thispost.

  4. windows – SetupDiGetClassDevs是否与文档中的设备实例ID一起使用?

    有没有更好的方法可以使用DBT_DEVICEARRIVAL事件中的数据获取设备的更多信息?您似乎必须指定DIGCF_ALLCLASSES标志以查找与给定设备实例ID匹配的所有类,或者指定ClassGuid并使用DIGCF_DEFAULT标志.这对我有用:带输出:

  5. Windows Live ID是OpenID提供商吗?

    不,WindowsLiveID不是OpenID提供商.他们使用专有协议.自从他们的“测试版”期结束以来,他们从未宣布计划继续它.

  6. 如果我在代码中进行了更改,是否需要重新安装Windows服务?

    我写了一个Windows服务并安装它.现在我对代码进行了一些更改并重新构建了解决方案.我还应该重新安装服务吗?不,只需停止它,替换文件,然后重新启动它.

  7. 带有双引号的字符串回显使用Windows批处理输出文件

    我正在尝试使用Windows批处理文件重写配置文件.我循环遍历文件的行并查找我想要用指定的新行替换的行.我有一个’函数’将行写入文件问题是%Text%是一个嵌入双引号的字符串.然后失败了.可能还有其他角色也会导致失败.如何才能使用配置文件中的所有文本?尝试将所有“在文本中替换为^”.^是转义字符,因此“将被视为常规字符你可以尝试以下方法:其他可能导致错误的字符是:

  8. .net – 将控制台应用程序转换为服务?

    我正在寻找不同的优势/劣势,将我们长期使用的控制台应用程序转换为Windows服务.我们为ActiveMQ使用了一个叫做java服务包装器的东西,我相信人们告诉我你可以用它包装任何东西.这并不是说你应该用它包装任何东西;我们遇到了这个问题.控制台应用程序是一个.NET控制台应用程序,默认情况下会将大量信息记录到控制台,尽管这是可配置的.任何推荐?我们应该在VisualStudio中将其重建为服务吗?我使用“-install”/“-uninstall”开关执行此操作.例如,seehere.

  9. windows – 捕获外部程序的STDOUT和STDERR *同时*它正在执行(Ruby)

    哦,我在Windows上:-(实际上,它比我想象的要简单,这看起来很完美:…是的,它适用于Windows!

  10. windows – 当我试图批量打印变量时,为什么我得到“Echo is on”

    我想要执行一个简单的批处理文件脚本:当我在XP中运行时,它给了我预期的输出,但是当我在Vista或Windows7中运行它时,我在尝试打印值时得到“EchoisOn”.以下是程序的输出:摆脱集合表达式中的空格.等号(=)的两侧可以并且应该没有空格BTW:我通常在@echo关闭的情况下启动所有批处理文件,并以@echo结束它们,所以我可以避免将代码与批处理文件的输出混合.它只是使您的批处理文件输出更好,更清洁.

返回
顶部