2013-02-18 42 views
4

我有一個自定義組合框,它應該對一些依賴屬性的值作出反應。 (例如驗證)。 現在,我只是無法找到如何更改控制模板的觸發器中的組合框控件的背景...我可以更改下拉菜單,但還沒有找到如何更改整個背景控制。依賴屬性都設置正確,這裏的XAML:更改自定義WPF組合框的背景

<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition Width="20" /> 
     </Grid.ColumnDefinitions> 
     <Border 
       x:Name="Border" 
       Grid.ColumnSpan="2" 
       CornerRadius="2" 
       Background="{StaticResource NormalBrush}" 
       BorderBrush="{StaticResource NormalBorderBrush}" 
       BorderThickness="1" /> 
     <Border 
       Grid.Column="0" 
       CornerRadius="2,0,0,2" 
       Margin="1" 
       Background="{StaticResource WindowBackgroundBrush}" 
       BorderBrush="{StaticResource NormalBorderBrush}" 
       BorderThickness="0,0,1,0" /> 
     <Path 
       x:Name="Arrow" 
       Grid.Column="1"  
       Fill="{StaticResource GlyphBrush}" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Data="M 0 0 L 4 4 L 8 0 Z"/> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="ToggleButton.IsMouseOver" Value="true"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" /> 
     </Trigger> 
     <Trigger Property="ToggleButton.IsChecked" Value="true"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" /> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /> 
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" /> 
      <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
      <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" /> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox"> 
    <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" /> 

</ControlTemplate> 

<Style x:Key="{x:Type local:ErgoDentComboBox}" TargetType="ComboBox" BasedOn="ComboBox"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <!--<Setter Property="OverridesDefaultStyle" Value="true"/>--> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
    <Setter Property="MinWidth" Value="120"/> 
    <Setter Property="MinHeight" Value="20"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:ErgoDentComboBox}"> 
       <Grid> 
        <ToggleButton 
         Name="ToggleButton" 
         Template="{StaticResource ComboBoxToggleButton}" 
         Grid.Column="2" 
         Focusable="false" 
         IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" 
         ClickMode="Press"> 
        </ToggleButton> 
        <ContentPresenter 
         Name="ContentSite" 
         IsHitTestVisible="False" 
         Content="{TemplateBinding SelectionBoxItem}" 
         ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
         ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
         Margin="3,3,23,3" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Left" /> 
        <TextBox x:Name="PART_EditableTextBox" 
          Style="{x:Null}" 
          Template="{StaticResource ComboBoxTextBox}" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Center" 
          Margin="3,3,23,3" 
          Focusable="True" 
          Background="Transparent" 
          Visibility="Hidden" 
          IsReadOnly="{TemplateBinding IsReadOnly}"/> 
        <Popup 
          Name="Popup" 
          Placement="Bottom" 
          IsOpen="{TemplateBinding IsDropDownOpen}" 
          AllowsTransparency="True" 
          Focusable="False" 
          PopupAnimation="Slide"> 
         <Grid 
            Name="DropDown" 
            SnapsToDevicePixels="True"     
            MinWidth="{TemplateBinding ActualWidth}" 
            MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
          <Border 
           x:Name="DropDownBorder" 
           Background="{StaticResource WindowBackgroundBrush}" 
           BorderThickness="1" 
           BorderBrush="{StaticResource SolidBorderBrush}"/> 
          <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> 
           <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
          </ScrollViewer> 
         </Grid> 
        </Popup> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="HasItems" Value="false"> 
         <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
        </Trigger> 
        <Trigger Property="IsGrouping" Value="true"> 
         <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
        </Trigger> 
        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true"> 
         <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/> 
         <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/> 
        </Trigger> 
        <Trigger Property="IsEditable" 
      Value="true"> 
         <Setter Property="IsTabStop" Value="false"/> 
         <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/> 
         <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/> 
        </Trigger> 
        <!-- THIS ISN'T WORKING --> 
        <Trigger Property="IsValidationError" Value="False"> 
         <Setter Property="BorderBrush" Value="DarkGreen" /> 
         <Setter Property="Background" Value="Green" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
    </Style.Triggers> 
</Style> 

在我可以很容易地設置背景控件的代碼,我怎麼會在觸發器做到這一點?

感謝

丹尼

回答

2

對於你真正想改變的東西,我還是有點困惑。我會假設它是ToggleButton背景。

既然你有一個單獨的ToggleButton的ControlTemplate,我建議你使用TemplateBinding,如果你想將你的ComboBox模板觸發器的顏色傳遞給ToggleButton模板。同樣的方法將適用於更改邊框顏色。

在你的組合框模板:

<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" 
       Grid.Column="2" Focusable="false" ClickMode="Press" 
       IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
       Background="{StaticResource WindowBackgroundBrush}"/> <!-- notice this line --> 


<Trigger Property="IsValidationError" Value="False"> 
    <Setter TargetName="DropDownBorder" Property="Background" Value="Yellow" /> <!-- changing dropdown background --> 
    <Setter TargetName="ToggleButton" Property="Background" Value="Yellow" /> <!-- changing togglebutton background --> 
</Trigger> 

在你切換按鈕模板:

<Border Grid.Column="0" CornerRadius="2,0,0,2" Margin="1" 
     BorderBrush="{StaticResource NormalBorderBrush}" BorderThickness="0,0,1,0" 
     Background="{TemplateBinding Background}" /> <!-- notice this line --> 
+0

其實我想改變的不僅僅是按鈕,但假設我只想改變按鈕,我該怎麼做?我想我並不需要覆蓋整個ComboBox模板,或者?我想我只是沒有完全理解模板的概念,完全覆蓋... – Daniel 2013-02-20 14:21:25

+0

不,在我的回答中,只有你需要改變的段落才包括在內(注意「注意這行」註釋)。你可以堅持你的模板。我也測試過它,爲我工作。 – 2013-02-20 14:24:01

+0

非常好,我設法實施你的解決方案,非常感謝你花時間......其實這正是我想要的,我似乎沒有表達得很好。 – Daniel 2013-02-20 14:35:47

0

我想你應該嘗試設置背景屬性爲您ContentSite(內容展示器,因爲它是一個誰顯示組合框的內容)。否則,一個解決方法的想法 - 用邊框包裝內容演示者,並設置爲免費。

+0

感謝您的建議。但是,contentpresenter沒有Background屬性,如果我只是在它周圍放置一個邊框(使用背景顏色),那麼我只是得到一個矩形而不是控件(使用已定義的背景色...) – Daniel 2013-02-18 08:27:51

0

Background屬性將會改變WPF Combobox的編輯和放下箭頭區域。 要更改其他顏色,如彈出式的背景和高亮顏色,你有一些筆刷添加到資源字典,映射到系統顏色:

var combo = new Combobox(); 
combo.Background = Brushes.Yellow; 
combo.Foreground = Brushes.Blue; 
combo.Resources.Add(SystemColors.WindowBrushKey, Brushes.Yellow); 
combo.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Red); 

<Combobox Background="Yellow" Foreground="Blue"> 
    <Combobox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" /> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" /> 
    </Combobox.Resources> 
</Combobox> 

資源:[鏈接] http://www.codeproject.com/Tips/84637/TIP-Change-background-of-WPF-Combobox

+0

更改編輯和下拉箭頭區域的組合框實際上就是我想要做的......特別是我想根據ControlTemplate中的屬性來做到這一點... – Daniel 2013-02-18 10:41:48

0

這裏就是我沒有...你有你「ContentPresenter」,我有「邊界」,並賦予它一個「名稱」值包礦等

<Border Name="presenterBorder" 
    Background="White" 
    BorderThickness="0" 
    BorderBrush="Transparent" 
    Margin="0" 
    Grid.Column="0" > 

    <ContentPresenter 
     Name="ContentSite" 
     IsHitTestVisible="False" 
     Content="{TemplateBinding SelectionBoxItem}" 
     ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
     ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
     Margin="3,3,23,3" 
     VerticalAlignment="Center" 
     HorizontalAlignment="Left" /> 
</Border> 

然後,改變你的觸發...在我的情況下,由於我沒有你有相同的財產d,我只是通過「IsMouseOver」進行模擬。我改變了顏色,但關鍵的是「TargetName」。您想要更改名爲「presenterBorder」的控件的背景和邊框,該控件是您用文本內容演示者包裹的邊框。

<Trigger Property="IsMouseOver" Value="True" > 
    <Setter Property="BorderBrush" Value="Red" TargetName="presenterBorder" /> 
    <Setter Property="Background" Value="Maroon" TargetName="presenterBorder" /> 
</Trigger> 
+0

這實際上聽起來是正確的,但是我得到一個白色框而不是ComboBox ...我的ComboBox模板在某種程度上可能有問題嗎?我對自定義控件比較陌生,實際上我只是想用額外的觸發器覆蓋組合框模板... – Daniel 2013-02-20 14:07:57

+0

@丹尼爾,如果你看看我的邊界,我明確地把白色,但你可能只需要改變它。讓我知道 – DRapp 2013-02-20 14:43:01

+0

好吧,即使任何其他顏色似乎然後覆蓋整個內容...如果它的透明,那麼我不能「着色」它下面的東西。我通過更改ToggleButton的背景找到了解決方案。無論如何,非常感謝您的幫助。 – Daniel 2013-02-20 14:46:12