---
# System prepended metadata

title: WinUI 3 でコンボボックスを使用する。 2022-07-26
tags: [INotifyPropertyChanged, WinUI 3, ComboBox, コンボボックス]

---

---
lang: ja-jp
breaks: true
---
# WinUI 3 でコンボボックスを使用する。 2022-07-26

## MainWindow.xaml

```xaml=
<Window
	x:Class="App25.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="using:App25"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	xmlns:viewModels="clr-namespace:App25.viewModels"
	>
	<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
		<ComboBox 
			Header="Nuget PackageSource Keys" 
			PlaceholderText="Nuget PackageSource Keys" 
			Width="400" 
			ItemsSource="{x:Bind ViewModel.PackageSourceKeys, Mode=OneTime}"
			SelectedValue="{x:Bind ViewModel.PackageSourceKey, Mode=TwoWay}"
			>
		</ComboBox>
	</StackPanel>
</Window>
```

## MainWindow.xaml.cs

```csharp=
namespace App25;

/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private MainWindowViewModel ViewModel { get; } = new MainWindowViewModel();
}
```

## MainWindowViewModel.cs

```csharp=
namespace App25.viewModels;

internal sealed class MainWindowViewModel:
    INotifyPropertyChanged
{
    public MainWindowViewModel()
        : base()
    {
    }

    public IEnumerable<string> PackageSourceKeys
    {
        get
        {
            return GetPackageSourceKeys();
        }
    }

    private IEnumerable<string> GetPackageSourceKeys()
    {
        NuGetUserConfig nuGetUserConfig = new();
        nuGetUserConfig.ParseXml();
        IReadOnlyDictionary<string, string> packageSources = nuGetUserConfig.PackageSources;
        return packageSources
            .OrderBy(n => n.Key)
            .Select(n => n.Key);
    }
    
    private string m_packageSourceKey;
    /// <summary>
    ///
    /// </summary>
    /// <param name="packageSourceKey "></param>
    public MainWindowViewModel(
        string packageSourceKey
        )
         : base()
    {
        this.m_packageSourceKey = packageSourceKey;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="org"></param>
    public MainWindowViewModel(
        MainWindowViewModel org
        )
         : base()
    {
        this.m_packageSourceKey = org.m_packageSourceKey;
    }
    public string PackageSourceKey { get { return this.m_packageSourceKey; } set { if (value == this.m_packageSourceKey) { return; } this.m_packageSourceKey = value; OnPropertyChanged(nameof(PackageSourceKey)); } }

    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangedEventHandler PropertyChanged_PackageSourceKey;
    private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        switch (propertyName)
        {
            case nameof(PackageSourceKey):
                PropertyChanged_PackageSourceKey?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                break;
        }
    }
    
}
```

![](https://i.imgur.com/eylkUMt.png)


###### tags: `WinUI 3` `コンボボックス` `ComboBox` `INotifyPropertyChanged`
