PhoneApplicationPage的动态控件

时间:2013-06-17 10:09:07

标签: wpf xaml windows-phone-8 wpf-controls

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

在伪代码i图像中它看起来像:

<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方法。

任何建议?

由于

1 个答案:

答案 0 :(得分: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;
            }

        }
    }