2011-09-28 75 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

實際上它仍然可以通過綁定完成,請參閱我的更新答案。 :) –