์ฐ์ ์ฝ๋ ๋จผ์ ๋ณด์ฌ ๋๋ฆฌ๋ฉด
// 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 ์ค์น ํ์๋ฉด ๋ฉ๋๋ค.