简体   繁体   English

WPF-超出项目集合的DataTemplate代码的可重用性

[英]WPF - Reusability of DataTemplate code out of items collection

I am using Bing maps in my WPF project, and I want to use common style overall project View with map. 我在WPF项目中使用Bing地图,并且想在地图中使用通用样式的整体项目“视图”。 It is not important that it's about Bing maps, problem here is in reusability of code defined in DataTemplate also for single object of ViewModel. 关于Bing地图并不重要,这里的问题是在DataTemplate中定义的代码的可重用性,也适用于ViewModel的单个对象。

In the folowing code, there is DataTemplate for images placed on map. 在以下代码中,有DataTemplate用于放置在地图上的图像。 It has some properties bind on ViewModel item from collection. 它具有绑定到ViewModel项目上的一些属性。

<DataTemplate x:Key="HeadingArrowsDataTemplate">
    <Image Source="/Intens.C2XTestPlatform.Frontend;component/Images/Maps/Arrow.png"
           m:MapLayer.Position="{Binding PointLocation}"
           RenderTransformOrigin="0.5,1"
           Width="100"
           Height="100"
           Visibility="{Binding Path=PointLocation, Converter={StaticResource IsNotNullVisibilityConverter}}">
        <Image.RenderTransform>
            <TransformGroup>
                <RotateTransform Angle="{Binding Heading}" />
                <TranslateTransform X="-50"
                                    Y="-100" />
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
</DataTemplate>

I use it in the first View for ViewModels collection ( RelevanceLinesVM ): 我在第一个ViewModels集合View( RelevanceLinesVM )中使用它:

    <m:Map CredentialsProvider="{Binding MapCredentialsProvider}"
           Mode="Road"
           x:Name="ucMap">
        <m:MapItemsControl ItemsSource="{Binding RelevanceLinesVM}"
                           helpers:MapFixBehavior.FixUpdate="True"
                           ItemTemplate="{StaticResource HeadingArrowsDataTemplate}"
    </m:Map>

In other View, I have only single ViewModel object ( RelevanceLineVM ), and I have to copy-paste code from DataTemplate for the same appearance and binding. 在其他View中,我只有一个ViewModel对象( RelevanceLineVM ),并且必须从DataTemplate复制粘贴代码以实现相同的外观和绑定。

   <m:Map Name="ucMap"
           Mode="Road"
           CredentialsProvider="{Binding MapCredentialsProvider}">
        <Image DataContext="{Binding RelevanceLineVM}"
               Source="/Intens.C2XTestPlatform.Frontend;component/Images/Maps/Arrow.png"
               RenderTransformOrigin="0.5,1"
               Width="100"
               Height="100"
               m:MapLayer.Position="{Binding PointLocation}"
               Visibility="{Binding Path=PointLocation, Converter={StaticResource IsNotNullVisibilityConverter}}">
            <Image.RenderTransform>
                <TransformGroup>
                    <RotateTransform Angle="{Binding Heading}" />
                    <TranslateTransform X="-50"
                                        Y="-100" />
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
    </m:Map>

How can I create common template for usage for both single ViewModel and collection of ViewModels? 如何创建用于单个ViewModel和ViewModels集合的通用模板?

Edit 编辑

ASh's answer works, but unfortunately only for these properties in DataTemplate, which are inherited from ContentControl class. ASh的答案有效,但不幸的是,仅适用于DataTemplate中的这些属性,这些属性是从ContentControl类继承的。 Other are not bound properly. 其他未正确绑定。

add ContentControl and reuse HeadingArrowsDataTemplate for its ContentTemplate 添加ContentControl并为其ContentTemplate重用HeadingArrowsDataTemplate

<m:Map Name="ucMap"
       Mode="Road"
       CredentialsProvider="{Binding MapCredentialsProvider}">
    <ContentControl Content="{Binding RelevanceLineVM}"
                    ContentTemplate="{StaticResource HeadingArrowsDataTemplate}"/>
</m:Map>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM