ComboBox์˜ ํด๋ฆญ

์šฐ์„  ์ฝ”๋“œ ๋จผ์ € ๋ณด์—ฌ ๋“œ๋ฆฌ๋ฉด

// MainWindow.xaml
<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="Window"
    SizeToContent="WidthAndHeight"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid Margin="4">
        <Grid MinWidth="50">
            <ComboBox
                IsEditable="True"
                IsReadOnly="True"
                ItemsSource="{Binding Items}">
                <ComboBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:ListItem}">
                        <CheckBox
                            Command="{Binding DataContext.ItemCheckChandedCommand, ElementName=Window}"
                            Content="{Binding Content}"
                            IsChecked="{Binding IsChecked}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock
                Margin="8,2,22,2"
                VerticalAlignment="Center"
                IsHitTestVisible="False"
                Text="{Binding SelectedItem}" />
        </Grid>
    </Grid>
</Window>
// MainViewModel.cs
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System.Collections.ObjectModel;

namespace WpfApp1;

public class MainViewModel : ReactiveObject
{
    public ObservableCollection<ListItem> Items { get; } = [];
    public IReactiveCommand ItemCheckChandedCommand { get; }
    [Reactive] public string SelectedItem { get; set; } = "";
    public MainViewModel()
    {
        ItemCheckChandedCommand = ReactiveCommand.Create(ItemCheckChanded);
        Items.Add(new() { Content = "1000" });
        Items.Add(new() { Content = "2000" });
        Items.Add(new() { Content = "3000" });
        Items.Add(new() { Content = "4000" });
        Items.Add(new() { Content = "5000" });
        ItemCheckChanded();
    }

    private void ItemCheckChanded()
    {
        SelectedItem = string.Join(", ", Items.Where(x => x.IsChecked).Select(x => x.Content));
    }
}
public class ListItem : ReactiveObject
{
    [Reactive] public bool IsChecked { get; set; } = true;
    [Reactive] public string Content { get; set; } = "Content";
    public override string ToString() => string.Empty;
}

๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ComboBox ์•„์ดํ…œ์„ ํŽผ์ณค์„ ๋•Œ
CheckBox์˜ Content ์˜์—ญ ๊นŒ์ง€๋ฅผ ํด๋ฆญ ํ–ˆ์„๋•
CheckBox ์˜ Check ๊ฐ’์ด ๋ฐ”๋€Œ๋Š”๋ฐ
๊ทธ ๋’ค๋ถ€๋ถ„์„ ํด๋ฆญ ํ–ˆ์„๋•
ComboBox๊ฐ€ ๋‹ซํžˆ๊ณ  ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ˜น์‹œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ์š”?

์ง์ ‘ ์‹คํ–‰ํ•ด ๋ณด์‹œ๋ ค๋ฉด ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์—

  <ItemGroup>
    <PackageReference Include="ReactiveUI" Version="20.1.63" />
    <PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
    <PackageReference Include="ReactiveUI.WPF" Version="20.1.63" />
  </ItemGroup>

์ถ”๊ฐ€ ํ•˜์‹œ๊ฑฐ๋‚˜ ์œ„ nuget ์„ค์น˜ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

1๊ฐœ์˜ ์ข‹์•„์š”
            <ComboBox
                IsEditable="True"
                IsReadOnly="True"
                ItemsSource="{Binding Items}">
                <ComboBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:ListItem}">
                        <CheckBox
                            Command="{Binding DataContext.ItemCheckChandedCommand, ElementName=Window}"
                            Content="{Binding Content}"
                            HorizontalAlignment="Stretch"
                            IsChecked="{Binding IsChecked}">
                        </CheckBox>
                    </DataTemplate>                    
                </ComboBox.ItemTemplate>
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>

์ถœ์ฒ˜ : wpf - How to stretch checkbox item the full width of the combobox - Stack Overflow

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
์‹ค์€ ์–ด์ œ ๊ธ€ ์˜ฌ๋ฆฌ๊ณ  ๋ช‡๋ถ„๋’ค์— ์ฐพ์•„์„œ ์ง€์šธ๋ ค๊ณ  ํ•ด๋ดค๋Š”๋ฐ ๊ธ€์„ ๋ชป์ง€์› ์Šต๋‹ˆ๋‹คโ€ฆใ…œ.ใ…œ

1๊ฐœ์˜ ์ข‹์•„์š”

๊ธ€์„ ์ง€์šฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€ ๊ณต์œ  ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ๊ณผ ๊ทธ ๊ธ€๋„ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ธฐ์—ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

4๊ฐœ์˜ ์ข‹์•„์š”