2016-01-21 105 views
1

正如在我上一個問題中,我將重新創建一個擴展器+它是切換按鈕。WPF ContentPresenter覆蓋其他控件在同一級別

擴展模板:

<ControlTemplate TargetType="Expander"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="15"/> 
     </Grid.RowDefinitions> 
     <ContentPresenter x:Name="ContentPresenter" Grid.Row="0"/> 
     <ToggleButton Grid.Row="1" > 
      .... 
     </ToggleButton> 
    </Grid> 

現在,當我用我的主要瀏覽此自定義控件,並添加一個控制(如按鈕):

<custom:FullWidthExpander Width="200" HeaderBackground="Gray"> 
    <Button /> 
</custom:FullWidthExpander> 

切換按鈕(在定義我的上面的Expander Template)被這個Button覆蓋。

+0

你肯定會被覆蓋,而不僅僅是隱藏的按鈕? 您可以使用[Snoop](https://snoopwpf.codeplex.com/)查看您的控件在加載和運行時的樣子。 – KinSlayerUY

+0

是的,當探聽我的應用程序時,除了ContentPresenter以外的所有東西都丟失了:/ – C4p741nZ

+0

如果你把一個大小放到你的按鈕上,它還會發生嗎?按鈕將獲取所有可用的大小,以便自動生成擴展器大小可能是 – nkoniishvt

回答

1

你綁定UserControl.ContentExpander.Content財產?

<!-- define a custom Template for the UserControl FullWidthExpander --> 
<UserControl.Template> 
    <ControlTemplate TargetType="UserControl"> 
     <!-- be sure to include the Content binding to pass the UC.Content to Expander --> 
     <Expander Content="{TemplateBinding Content}"> 
      <!-- create a custom Template for this Expander --> 
      <Expander.Template> 
       <ControlTemplate TargetType="Expander"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition x:Name="ContentRow" Height="*"/> 
          <RowDefinition Height="20"/> 
         </Grid.RowDefinitions> 
         <ContentPresenter Grid.Row="0" Content="{TemplateBinding Content}"/> 
         <ToggleButton Grid.Row="1" Content="Test Toggle Button" /> 
        </Grid> 
       </ControlTemplate> 
      </Expander.Template> 
     </Expander> 
    </ControlTemplate> 
</UserControl.Template> 

我做了一個快速測試,這使用這樣的控制時,正常工作:

<local:FullWidthExpander> 
    <Button Content="Test Content Button"/> 
</local:FullWidthExpander> 

enter image description here

我也看了看your other question,如果你有你的ControlTemplate.Triggers在這裏,您還需要確保將Expander.IsExpanded設置爲True以查看您的內容。如果IsExpanded=False(這是擴展器的默認值),則觸發器將隱藏頂部內容行。

+0

謝謝您的回覆:) - 如果我解決了這個問題,這個解決方案「只是一個骯髒的解決方法」。爲了澄清,我只是想製作一個具有「新外觀和感覺」的可重複使用的Expander。你可以告訴我什麼是最佳實踐解決方案? (我不想侮辱你告訴它是一種變通方法 - 但據我瞭解,用戶控件對我來說不是最好的) – C4p741nZ

+0

@ Chill-X在你的情況下,是的,它可能更好使用一個自定義模板爲您的擴展器比一個全新的用戶控件,但核心問題將是相同的,無論如何。它很好地知道原因和解決方案,所以你不會遇到這樣的問題在未來:) – Rachel

1

當您這樣做時,這意味着用戶控件的全部內容將被替換爲您提供的內容。

<custom:FullWidthExpander Width="200" HeaderBackground="Gray"> 
    <Button /> 
</custom:FullWidthExpander> 

要解決這個問題,你需要以不同的承載內容點點在你的用戶控件

加上一個依賴屬性的用戶控件

public object UserControlContent 
    { 
     get { return (object)GetValue(UserControlContentProperty); } 
     set { SetValue(UserControlContentProperty, value); } 
    } 

    public static readonly DependencyProperty UserControlContentProperty = 
     DependencyProperty.Register("UserControlContent", typeof(object), typeof(FullWidthExpander), 
      new PropertyMetadata(null)); 

那麼這個綁定到contentpresenter在usercontrol的xaml也爲你的用戶控件定義了一個名字,比如x:Name =「Root」。

<Expander Header="My expander header"> 
    <Expander.Template> 
     <ControlTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="15"/> 
       </Grid.RowDefinitions> 

       <ContentPresenter x:Name="ContentPresenter" Grid.Row="0" Content="{Binding UserControlContent, ElementName=Root}" /> 

       <ToggleButton Grid.Row="1" Content="Togglebutton"> 

       </ToggleButton> 
      </Grid> 
     </ControlTemplate> 
    </Expander.Template> 
</Expander> 

最後你定義maindwindow XAML內容含量等

<custom:FullWidthExpander> 
    <custom:FullWidthExpander.UserControlContent> 
     <Button Content="Click me"/> 
    </custom:FullWidthExpander.UserControlContent> 
</custom:FullWidthExpander> 
+0

感謝您的回答 - 但我認爲ContentPresenter是我提供的內容的某種「佔位符」? – C4p741nZ

+0

對於模板是,用戶控制編號。 –

+0

@JanneMatikainen他從來沒有說任何關於UserControl – nkoniishvt