2011-03-30 31 views
0

,我有以下的自定義用戶控件:如何讓我的自定義WPF usercontrol綁定到內容屬性?

<UserControl x:Class="TestGUI.TBorder"> 
<Border Style="{StaticResource brdListBoxItem}"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="35" /> 
      <RowDefinition Height="5" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <TextBlock Grid.Row="0" 
        Grid.Column="0" 
        Style="{StaticResource tblTitleDataStyle}" 
        Text="{Binding Header, Mode=OneWay}" /> 

     <Rectangle Grid.Row="1" 
        Grid.Column="0" 
        Margin="5,2" 
        Fill="{StaticResource BlueTextBrush}" 
        Height="1" 
        HorizontalAlignment="Stretch" /> 

     <ContentPresenter Name="ccpMain" 
         Grid.Row="2" 
         Grid.Column="0" 
         Content="{Binding Content}" /> 
     <!--<TextBlock Grid.Row="2" 
        Text="TEST HERE" />--> 
    </Grid> 

</Border> 

如果我註釋掉「ContentPresenter」,並取消了「TextBlock的」,這似乎符合市場預期。如果我擁有它,那麼只有顯示的內容是標籤內的任何內容。例如:

<local:TBorder Grid.Row="2" 
         Grid.Column="0" Width="300" 
         Header="The Header"> 
     <TextBlock Text="astnouhe" /> 
    </local:TBorder> 

只顯示「TextBlock」。

有人可以向我解釋這一點嗎?

感謝您的任何幫助。

回答

1

你是如何設定內容的?如果您將其設置爲您在問題中使用的XAML,那麼它正在嘗試在控件中顯示文本本身。

您需要創建另一個在您的答案中使用XAML的控件,並將其設置爲用戶控件的內容,以便在用戶控件中顯示這些控件。

+0

我明白你在第一段中的意思,但是第二段中的解決方案讓我失去了意識。對不起,密集。 – Sako73 2011-03-30 19:09:46

+0

@ Salko73沒關係。 :)基本上,你不能只將某些XAML設置爲屬性,並讓WPF呈現控件 - 您需要將實際控件設置爲content屬性,以便將它們呈現在屏幕上。這有幫助嗎? – Andy 2011-03-30 19:10:49

+0

它確實有道理。爲了將來的參考,這個鏈接幫助我將其轉化爲實際的代碼:http://10rem.net/blog/2010/02/05/creating-customized-usercontrols-deriving-from-contentcontrol-in-wpf-4感謝您的幫幫我。 – Sako73 2011-03-30 19:34:56

相關問題