2011-09-28 27 views
1

您好我想作一个简单的用户控件用户控件可混合性WP7

<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(); 
    } 
} 

现在所有的作品,但不是共混......和苹果酒,我可以”看不到“测试的消息”

有一个可行的办法:)没有涉及的xmlns:MyControl = ...

+0

你必须更清楚你定义的“Blendable”。 **精确**不起作用。 –

+0

我的意思是我在设计时看到在运行应用程序之前在屏幕上显示“在消息中测试”消息:) – LXG

+0

您错误地理解了依赖属性如何完全工作。 –

回答

1

大多数人如果你可以编辑它的模板,那么控件就是Blendable。为此,您必须将其从用户控件更改为自定义控件,以使其模板在gerenic.xaml中定义。

但是,从您的意见,它听起来像你需要设计时数据,而不是能够使控制Blendable。看看design-time attributes in Silverlight上的MSDN部分。特别是d:DataContext,这在WP7中工作得很好。

0

除了可啉的答案,你可能需要改一下你的代码,让您的依赖属性与设计时数据的工作,

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}"的结合。

要显示在设计时的文字,你需要添加一个设计时间DataContext的(像什么可啉建议),

<Grid x:Name="LayoutRoot" d:DataContext="{d:DesignInstance design:DesignMainViewModel, IsDesignTimeCreatable=True}"> 
    <xxx:TestControl testMessage={Binding SomeText} /> 
</Grid> 

希望这有助于。 :)

编辑

其实科林指出的那样,你不需要回调,如果你的名字你UserControl和使用的ElementName绑定,而不是正常的结合。

<UserControl x:Name="myUserControl" ... 

TextBlock的内部,你做

Text="{Binding testMessage, ElementName=myUserControl}" 

只需绑定到测试消息是行不通的,因为这个属性是用户控件的。

+0

出于兴趣,您为什么认为DP PropertyChnagedCallback是需要的? XAML Text =“{Binding testMessage}”中的绑定将确保textBlock1获取设计时数据。 – ColinE

+0

请纠正我,如果我错了,但我不认为TextBlock可以访问UserControl的依赖项属性? –

+0

实际上它仍然可以通过绑定完成,请参阅我的更新答案。 :) –