0

私が行ったのは、左の画像に示されている効果です。2つのデータグループを含むコンボボックスがあります。コンボボックスで項目を選択すると、表示されるテキストは右の画像のようになります(いくつか追加します)たとえば、グループ Aで項目 ' 00 'を選択すると、表示されるテキストは ' A_00 ' になります。コードに基づいてこの効果を実現するにはどうすればよいですか? ここに画像の説明を入力

GroupComboBoxResDic.xaml 内

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:helper="clr-namespace:WpfApplication3">

    <!-- Main style for ComboBox -->
    <Style x:Key="MyComboBox" TargetType="{x:Type ComboBox}">        

        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="150" Height="Auto" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Item, Converter={StaticResource terminalToDisplayNameConverter}, Mode=OneWay}" Width="40"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Style for ComboBoxItem -->
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Width" Value="50" />
    </Style>

    <!-- Style for ItemContainerStyle -->
    <Style x:Key="ComboBoxItemContainerStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding Available}" />
    </Style>

    <!-- DataTemplate for HeaderTemplate -->
    <DataTemplate x:Key="MyHeaderTemplate">
        <Border BorderBrush="Black" BorderThickness="2">
            <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" Background="YellowGreen" />
        </Border>
    </DataTemplate>

</ResourceDictionary>

XAML ファイル

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication3">

    <Window.Resources>
        <ResourceDictionary Source="GroupComboBoxResDic.xaml" />
    </Window.Resources>

    <Grid>
        <ComboBox x:Name="MyComboBox1" 
                  Style="{StaticResource MyComboBox}" 
                  IsSynchronizedWithCurrentItem="False" 
                  ItemContainerStyle="{StaticResource ComboBoxItemContainerStyle}"                  
                  ItemsSource="{Binding InputsList}" Margin="293,50,104,234">
            <ComboBox.GroupStyle>
                <GroupStyle HeaderTemplate="{StaticResource MyHeaderTemplate}" />
            </ComboBox.GroupStyle>
        </ComboBox>
    </Grid>
</Window>

コード ビハインド ファイル

public enum CategoryGroup
    {
        GroupA = 0,

        GroupB,
    }


    public class CategoryItemString
    {
        public string Item { get; set; }
        public bool Available { get; set; }

        public CategoryGroup Group { get; set; }

        public string Category
        {
            get
            {
                switch (Group)
                {
                    case CategoryGroup.GroupA:
                        return "Group A";
                    case CategoryGroup.GroupB:
                        return "Group B";
                }

                return string.Empty;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            CategoryItemString rht = obj as CategoryItemString;
            if (rht == null) return false;

            return rht.Item.Equals(this.Item) &&
                rht.Group == this.Group;
        }
    }


    public class VM : INotifyPropertyChanged
    {
        private ListCollectionView lcv;

        public VM()
        {

            List<CategoryItemString> items = new List<CategoryItemString>();

            for (int i = 0; i < 18; ++i)
            {
                items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupA });
            }

            for (int i = 0; i < 4; ++i)
            {
                items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupB });
            }

            //Need the list to be ordered by the category or you might get repeating categories
            lcv = new ListCollectionView(items.OrderBy(w => w.Category).ToList());

            //Create a group description
            lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
        }

        public ListCollectionView InputsList
        {
            get { return lcv; }
            set
            {
                if (lcv != value)
                {
                    lcv = value;
                    OnPropertyChanged("InputsList");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string prop)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

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

            VM vm = new VM();

            this.MyComboBox1.DataContext = vm;
        }
    }
4

1 に答える 1