Xaml 用户控件可混合性wp7

Xaml 用户控件可混合性wp7,xaml,windows-phone-7,user-controls,Xaml,Windows Phone 7,User Controls,嗨,我想做一个简单的用户控件 <UserControl x:Class="TestDependencyProps.controls.TestControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.c

嗨,我想做一个简单的用户控件

<UserControl x:Class="TestDependencyProps.controls.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" >
        <TextBlock Height="30" Margin="31,140,27,0" Name="textBlock1" Text="{Binding testMessage}" VerticalAlignment="Top" />
    </Grid>

</UserControl>
现在所有的都可以工作,但不能混合。。。在苹果酒中,我看不到“消息中的测试”


有一种方法可以工作:)不涉及xmlns:MyControl=…

大多数人认为,如果可以编辑控件的模板,则该控件是可混合的。为此,必须将其从用户控件更改为自定义控件,以便在gerenic.xaml中定义其模板


但是,从您的评论来看,似乎您需要设计时数据,而不是使控件可混合。请查看上的MSDN部分。特别是
d:DataContext
,它在WP7中工作得很好。

大多数人认为,如果可以编辑控件的模板,则该控件是可混合的。为此,必须将其从用户控件更改为自定义控件,以便在gerenic.xaml中定义其模板


但是,从您的评论来看,似乎您需要设计时数据,而不是使控件可混合。请查看上的MSDN部分。特别是
d:DataContext
,它在WP7中工作得很好。

除了ColinE的答案之外,您可能需要更改一些代码,以使依赖项属性与设计时数据一起工作

    public string testMessage
    {
        get { return (string)GetValue(testMessageProperty); }
        set { SetValue(testMessageProperty, value); }
    }

public static readonly DependencyProperty testMessageProperty =
            DependencyProperty.Register("testMessage", typeof(string), typeof(TestControl), new PropertyMetadata("test in a message", PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var c = (TestControl)sender;
                // assign value to the TextBlock here
                c.textBlock1.Text = e.NewValue.ToString();
            }
        }
并删除TextBlock
Text=“{binding testMessage}”
中的绑定

要在设计时显示文本,需要添加一个设计时数据上下文(如ColinE所建议的)


简单地绑定到testMessage将不起作用,因为此属性属于UserControl。

除了ColinE的答案之外,您可能还需要更改一些代码,以使依赖项属性能够处理设计时数据

    public string testMessage
    {
        get { return (string)GetValue(testMessageProperty); }
        set { SetValue(testMessageProperty, value); }
    }

public static readonly DependencyProperty testMessageProperty =
            DependencyProperty.Register("testMessage", typeof(string), typeof(TestControl), new PropertyMetadata("test in a message", PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var c = (TestControl)sender;
                // assign value to the TextBlock here
                c.textBlock1.Text = e.NewValue.ToString();
            }
        }
并删除TextBlock
Text=“{binding testMessage}”
中的绑定

要在设计时显示文本,需要添加一个设计时数据上下文(如ColinE所建议的)


简单地绑定到testMessage是行不通的,因为该属性属于UserControl。

您必须更清楚地定义什么是“可混合的”。什么精确不起作用。我的意思是,在运行应用程序之前,我在设计时看到屏幕上的消息“在消息中测试:)你完全误解了依赖属性的工作原理。你必须更清楚地定义什么是“可混合”。什么精确不起作用。我的意思是,在运行应用程序之前,我在设计时看到屏幕上的消息“在消息中测试:)你完全误解了依赖属性的工作原理。只是出于兴趣,你为什么认为需要DP PropertyChnagedCallback?XAML Text=“{binding testMessage}”中的绑定将确保textBlock1拾取设计时数据。如果我错了,请纠正我,但我认为TextBlock无法访问UserControl的依赖属性?实际上它仍然可以通过绑定完成,请查看我的更新答案。:)哎呀,科林,我刚刚意识到我必须要“你”才能让你看到我的回答出于兴趣,您认为为什么需要DP PropertyChnagedCallback?XAML Text=“{binding testMessage}”中的绑定将确保textBlock1拾取设计时数据。如果我错了,请纠正我,但我认为TextBlock无法访问UserControl的依赖属性?实际上它仍然可以通过绑定完成,请查看我的更新答案。:)哎呀,科林,我刚刚意识到我必须要“你”才能让你看到我的回答
<Grid x:Name="LayoutRoot" d:DataContext="{d:DesignInstance design:DesignMainViewModel, IsDesignTimeCreatable=True}">
    <xxx:TestControl testMessage={Binding SomeText} />
</Grid>
<UserControl x:Name="myUserControl" ...
Text="{Binding testMessage, ElementName=myUserControl}"