组样式标题永远不会出现

时间:2009-07-09 08:37:28

标签: wpf styles groupstyle

我的GroupStyle标题永远不会出现在组合框中....

分组工作正常......这是一个具有约束力的问题,但我无法弄明白。

<ComboBox Height="23" Margin="33,45,125,0" Name="comboBox1" VerticalAlignment="Top"  ItemsSource="{Binding}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Border Background="Red">
                <TextBlock Text="{Binding Path=value}" />
            </Border>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock FontSize="12" FontWeight="Bold" Foreground="DarkGray">
                            <Button Content="{Binding Path=Location}"/>
                            <TextBlock  Text="{Binding Path=Location}" />
                            <Button>bbbb</Button>

                        </TextBlock>
                        <ItemsPresenter/>
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>
</ComboBox>

和背后的代码

public class Store
{
    public string Location { get; set; }
    public string value { get; set; }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        var myData = new ObservableCollection<Store>
        {
            new Store { Location = "Group1", value = "Item 1" },
            new Store { Location = "Bombay", value = "Item 2" },
            new Store { Location = "Group2", value = "Item 11" }
        }

        ICollectionView view = CollectionViewSource.GetDefaultView(myData);
        view.GroupDescriptions.Add(new PropertyGroupDescription("Location"));

        DataContext = myData;
    }
}

1 个答案:

答案 0 :(得分:10)

Try changing the Binding Path from "Location" to "Name"

<GroupStyle.HeaderTemplate>
   ...
    <Button Content="{Binding Path=Location}"/>
    <TextBlock  Text="{Binding Path=Location}" />
   ...
<GroupStyle.HeaderTemplate>

......像这样...

<GroupStyle.HeaderTemplate>
   ...
    <Button Content="{Binding Path=Name}"/>
    <TextBlock  Text="{Binding Path=Name}" />
   ...
<GroupStyle.HeaderTemplate>