用户控件可混合性 wp7控件、混合性、用户

2023-09-07 15:33:24 作者:命里不缺狗,你要走就走

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

Hi I'd like to make a simply user control

<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>

后面的代码:

public partial class TestControl : UserControl
{
    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",null)
        );

    public TestControl()
    {
        InitializeComponent();
    }
}

现在一切正常,但不可混合......在 Cider 中我看不到消息中的测试"

now all works but is not blendable ... and in Cider I can't see "test in a message"

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

there's a way that works :) without involve xmlns:MyControl=...

推荐答案

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

Most people consder that a control is Blendable if you can edit its template. To do this, you will have to change it from a user-control to a custom-control so that its template is defined in gerenic.xaml.

但是,从您的评论看来,您需要设计时数据,而不是能够使控件可混合.查看 MSDN 中关于 design-time attributes 的部分银光.特别是 d:DataContext,这在 WP7 中工作得很好.

However, from your comments it sounds like you need design-time data, rather than to be able to make the control Blendable. Take a look at the MSDN section on design-time attributes in Silverlight. Specifically d:DataContext, this works just fine in WP7.