2016-10-11 64 views
2

我創建了一個自定義的WPF組合框是這樣的:WPF自定義組合框顯示「System.Windows.Styles」上項目

<ComboBox 
    x:Name="ComboBoxBtn" 
    VerticalAlignment="Top" 
    HorizontalAlignment="Left" 
    Margin="0,0,0,-1" 
    Width="300" 
    ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    SelectedItem="{Binding Path=Selected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" 
     IsSynchronizedWithCurrentItem="True"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <ContentControl> 
        <Style TargetType="ContentControl"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding Favorite}" Value="True"> 
           <Setter Property="Content"> 
            <Setter.Value> 
             <Grid> 
              <Grid.Style> 
               <Style TargetType="Grid"> 
                <Setter Property="Background" Value="#FFE6E6FA"/> 
               </Style> 
              </Grid.Style> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="Auto" /> 
               <ColumnDefinition Width="Auto" /> 
              </Grid.ColumnDefinitions> 
              <Label Content="{Binding}" Width="250" /> 
              <Button Grid.Column="1" Command="{Binding AddCommandButton, ElementName=root}" 
               CommandParameter="{Binding}">+</Button> 
             </Grid> 
            </Setter.Value> 
           </Setter> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding Favorite}" Value="False"> 
           <Setter Property="Content"> 
            <Setter.Value> 
             <Grid> 
              <Grid.Style> 
               <Style TargetType="Grid"> 
                <Setter Property="Background" Value="Yellow"/> 
               </Style> 
              </Grid.Style> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="Auto" /> 
               <ColumnDefinition Width="Auto" /> 
              </Grid.ColumnDefinitions> 
              <Label Content="{Binding}" Width="250" /> 
              <Button Grid.Column="1" Command="{Binding RemoveCommandButton, ElementName=root}" 
               CommandParameter="{Binding}">-</Button> 
             </Grid> 
            </Setter.Value> 
           </Setter> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </ContentControl> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

的源綁定包含ObservableColleciton在客戶端有一個收藏領域,是爲了顯示不同的背景和不同的按鈕。但是,當我打開組合框的所有項目有:

「System.Windows.Styles」

回答

3

你的問題是要設置樣式爲內容控制內容。

<ContentControl> 
    <Style TargetType="ContentControl"> 
    ... 
    </Style> 
</ContentControl> 

爲了設置內容控件的樣式,你應該加上:

<ContentControl.Style> 

所以上面的代碼看起來就像這樣:

<ContentControl>  
    <ContentControl.Style> 
     <Style TargetType="ContentControl"> 
     ... 
     </Style>  
    <ContentControl.Style> 
</ContentControl> 

編輯

通過您的代碼對於更改背景的簡單任務非常複雜。這裏是一個簡化的版本:

<ComboBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Name="PART_GRID" Background="Yellow"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Label Content="{Binding}" 
         Width="250" /> 
       <Button Name="PART_BUTTON" 
         Grid.Column="1" 
         Content="-" 
         Command="{Binding AddCommandButton, ElementName=root}" 
         CommandParameter="{Binding}" /> 
      </Grid> 
      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding Favorite}" 
          Value="True"> 
        <Setter TargetName="PART_GRID" 
          Property="Background" 
          Value="#FFE6E6FA" /> 
        <Setter TargetName="PART_BUTTON" 
          Property="Content" 
          Value="+" /> 
        <Setter TargetName="PART_BUTTON" 
          Property="Command" 
          Value="{Binding RemoveCommandButton, ElementName=root}" /> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
+0

感謝您的幫助!就是這樣。現在,收藏夾= True的項目正在列表中正確顯示。那些顯示所有白色內容的「虛假」區域,但是如果我選擇它,則顯示正常。你知道爲什麼嗎? –

+0

@AndreRoque不知道爲什麼它不能在你的代碼中工作。但是增加了一個例子,我簡化了它並用一組布爾測試了它,它工作正常。 –

+0

問題是,我有兩個不同的按鈕,每種情況。編輯我的代碼 –

0

以我爲例,我發現我有:

<ComboBox> 
    <Style> 
    </Style> 
</ComboBox> 

這直接增加了風格的項目,類似於上述的解決。