0

我想显示模型的详细信息页面。根据模型的类型(定义为字符串属性),我想显示某些控件(图像、文本、媒体 ..)

在我的伪代码中,它看起来像:

<phone:PhoneApplicationPage
    x:Class="TestApp.FullscreenArtifactPage"
    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:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="False"
    xmlns:testapp="clr-namespace:TestApp"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
>
    <!-- if(model.type == TEXT) -->
     <StackPanel>
        <TextBlock Name="MimeText" Text="{Binding Mime}"/>
    </StackPanel>

    <!-- else if(model.type == IMAGE) -->

    <StackPanel>
        <Image Name="Image" Source="{Binding PayloadUri}"/>
    </StackPanel>

</phone:PhoneApplicationPage>

我知道我可以在 ListBoxes 中使用DataTemplateSelector,但是由于我这里没有 ListBox,所以没有调用 onChangeContent 方法。

有什么建议么?

谢谢

4

1 回答 1

0

你甚至可以丢弃ContentControl在你的XAML,而不是ControlTemplate你需要使用DataTemplate

    <testapp:FullscreenTypeSelector CurrentItem="{Binding YourItem}">
        <testapp:FullscreenTypeSelector.ImageTemplate>
            <DataTemplate>
                <StackPanel>
                    <Image Name="Image" Source="{Binding PayloadUri}"/>
                </StackPanel>
            </DataTemplate>
        </testapp:FullscreenTypeSelector.ImageTemplate>
        <testapp:FullscreenTypeSelector.TextTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Name="MimeText" Text="{Binding Mime}"/>
                </StackPanel>
            </DataTemplate>
        </testapp:FullscreenTypeSelector.ImageTemplate>
    </testapp:FullscreenTypeSelector>

并以这种方式定义您的选择器

 public class FullscreenTypeSelector : ContentControl
    {
        public static readonly DependencyProperty CurrentItemProperty =
            DependencyProperty.Register("CurrentItem", typeof (object), typeof (FullscreenTypeSelector), new PropertyMetadata(default(object)));

        public object CurrentItem
        {
            get
            {
                return (object) GetValue(CurrentItemProperty);
            }
            set
            {
                SetValue(CurrentItemProperty, value);
            }
        }

        public bool IsNote
        {
            get;
            set;
        }

        public DataTemplate ImageTemplate
        {
            get;
            set;
        }

        public DataTemplate TextTemplate
        {
            get;
            set;
        }

        public override void OnApplyTemplate()
        {
            //your condition goes here
            if (CurrentItem != null)
            {
                ContentTemplate = TextTemplate;
            }
            else
            {
                ContentTemplate = ImageTemplate;
            }

        }
    }   
于 2013-06-17T10:15:41.267 回答