2011-05-23 81 views
0

我想創建一個自定義控件繼承WPF電網這些功能:WPF自定義網格默認內容

  • 必須默認
  • 有一些行必須有一定的子控件在 那些行(主要是按鈕和行) 默認
  • 非默認內容 必須編輯由設計師或在控制插入

我試圖繼承網格,並在構造函數中添加內容,但是窗口的XAML 書面方式的內容,只要我添加設計師提供更多內容,默認內容丟失。我嘗試了很多東西,但我無法做到。它甚至有可能嗎?我怎麼能做到這一點?

回答

1

馬丁,你不能這樣做。 網格是一種面板,內容爲兒童屬性。所以,如果您在XAML設計器中添加了任何內容,它將被重新調整。

但是你可以重寫子女的財產,這在本例中添加到您的<ContentProperty("PropertyName")>類像 -

例:

'Code: 
<ContentProperty("Children")> _ 
Public Class MyGrid 
Public Overloads ReadOnly Property Children As UIElementCollection 
    Get 
     Return Me.ContentGrid.Children 
    End Get 
End Property 
End Class 

'Markup 
<Grid x:Class="MyGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Grid.RowDefinitions> 
    <RowDefinition /> 
    <RowDefinition /> 
    <RowDefinition /> 
</Grid.RowDefinitions> 
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" /> 
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="2" /> 
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="1" /> 

<Grid Name="ContentGrid" Grid.RowSpan="3"></Grid> 
</Grid> 
+0

真棒......我已經嘗試這樣做,做工不錯。我需要調整一下。我希望dev用戶能夠將控件放在特定的行中,因爲它與他看到的是同一個網格。非常感謝 – Martin 2011-05-23 23:38:18

+0

@ Code0987 +1來自未來,絕對是一個有用的答案,並教會了我一些我不知道的東西!這應該被標記爲答案。 – Feign 2015-02-11 19:23:21