2013-03-24 38 views
1

我試圖實現following code,區別在於我希望這適用於一種風格,以便我可以將其設置爲我喜歡的任何ComboBox(即由於特定的不變要求,我從後面的代碼中動態地創建了一些ComboBoxes,並且希望將GroupStyles添加到它們中的每個)。問題創建風格設置爲一些組合框的GroupStyle

我是比較新的WPFXAML,所以我想通過Style這樣做,並在ControlTemplate指定GroupStyles,然後只是應用樣式到相應的ComboBoxes。這是我到目前爲止所嘗試的,但代碼不會編譯(主要是由於<ComboBox.GroupStyle>部分)。

<Style x:Name="valuesComboStyle" TargetType="ComboBox"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate> 
          <ComboBox.GroupStyle> 
           <GroupStyle> 
            <GroupStyle.HeaderTemplate> 
             <DataTemplate> 
              <TextBlock Text="{Binding Name}"/> 
             </DataTemplate> 
            </GroupStyle.HeaderTemplate> 
           </GroupStyle> 
          </ComboBox.GroupStyle> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 

回答

1

定義在資源DataTemple地方。並將其用於您需要的每個Combobox

下面是代碼:

<Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="groupStyle"> 
      <TextBlock FontWeight="Bold" Text="{Binding Name}"/> 
     </DataTemplate> 
     <Style TargetType="{x:Type ComboBoxItem}" x:Key="comboBoxItemStyle"> 
      <Setter Property="Template" > 
       <Setter.Value> 
        <ControlTemplate> 
         <Label Background="Red" Content="{Binding Item}"/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
    <ComboBox Height="27" Width="195" DisplayMemberPath="Item" Name="cboGroup" 
       ItemContainerStyle="{StaticResource comboBoxItemStyle}"> 
     <ComboBox.GroupStyle> 
      <GroupStyle HeaderTemplate="{StaticResource groupStyle}"/> 
     </ComboBox.GroupStyle> 
    </ComboBox> 
</Grid> 

編輯:我創建了一個新的組合框,並設置一些項目,並設置你正在尋找的風格。 (我更新了你鏈接中的代碼)

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 


     ComboBox comboBox1 = new ComboBox(); 
     comboBox1.Height = 23; 
     comboBox1.Width = 200; 

     GroupStyle style = new GroupStyle(); 
     style.HeaderTemplate = (DataTemplate)this.FindResource("groupStyle"); 
     comboBox1.GroupStyle.Add(style); 
     comboBox1.DisplayMemberPath = "Item"; 
     ObservableCollection<CategoryItem<string>> items = new ObservableCollection<CategoryItem<string>>(); 

     items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Orange" }); 
     items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Red" }); 
     items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Pink" }); 
     items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Blue" }); 
     items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Purple" }); 
     items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Green" }); 

     CollectionViewSource cvs = new CollectionViewSource(); 
     cvs.GroupDescriptions.Add(new PropertyGroupDescription("Category")); 
     cvs.Source = items; 

     Binding b = new Binding(); 
     b.Source = cvs; 
     BindingOperations.SetBinding(
      comboBox1, ComboBox.ItemsSourceProperty, b); 

     myGrid.Children.Add(comboBox1); 
    } 
} 

public class CategoryItem<T> 
{ 
    public T Item { get; set; } 
    public string Category { get; set; } 
} 
+0

感謝您的建議 - 我怎麼能運用它爲每一個組合框,我需要?你能否展示一些示例C#代碼? – 2013-03-24 12:48:53

+0

對於您應該通過'StaticResource'結合設置它'GroupStyle'的'HeaderTemplate'財產每組合框。我要編輯我的代碼... – 2013-03-24 12:51:06

+0

這可以通過後面的代碼應用,或只有XAML?正如我試圖通過代碼背後所做的那樣。謝謝 – 2013-03-24 12:52:05

1

GroupStyle屬性是基於組合框,所以你需要單獨,而不是將其設置爲模板 -

<Style TargetType="ComboBox"> 
      <Setter Property="GroupStyle"> 
       <Setter.Value> 
        <GroupStyle> 
         <GroupStyle.HeaderTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding Name}"/> 
          </DataTemplate> 
         </GroupStyle.HeaderTemplate> 
        </GroupStyle> 
       </Setter.Value> 
      </Setter> 
     </Style> 

編輯

嗯,你不能設置從GroupStyle財產Style,因爲它沒有任何關聯的setter。

但是,您可以使用Add()方法(如解釋here)從代碼隱藏中添加它,或者您必須創建自定義Attached property解釋here。背後

碼 -

 GroupStyle g = new GroupStyle(); 

     //Create header template 
     FrameworkElementFactory control = new 
            FrameworkElementFactory(typeof(TextBlock)); 
     Binding binding = new Binding(); 
     control.SetBinding(TextBlock.TextProperty, binding); 
     binding.Path = new PropertyPath("Name"); 
     DataTemplate dataTemplate = new DataTemplate(); 
     dataTemplate.VisualTree = control; 

     g.HeaderTemplate = dataTemplate; 
     ComboBox cmb = new ComboBox(); 
     cmb.GroupStyle.Add(g); 
+0

我無法這樣做 - 它報告GroupStyle不是DependencyProperty。 – 2013-03-24 12:22:30

+0

更具體地說:'錯誤屬性「GroupStyle」不是一個DependencyProperty。要在標記中使用,必須使用可訪問的實例屬性「GroupStyle」在目標類型上公開非附屬屬性。對於附加的屬性,聲明類型必須提供靜態的「GetGroupStyle」和「SetGroupStyle」方法。' – 2013-03-24 12:25:43

+0

'GroupStyle'是一個DP,但不能通過'Style'來設置,因爲它沒有任何setter。但是,您可以通過代碼進行設置。詳情請參閱這裏asnwer - http://stackoverflow.com/questions/12022529/unable-to-set-the-groupstyle-for-a-listbox-via-style – 2013-03-24 12:32:07