Last active
December 2, 2017 08:16
-
-
Save yoshiiz/24d12b7d147f8439cd9f973df2c41cf4 to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfMutuallyExclusiveCheckableMenuItemsSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Data; | |
using System.Globalization; // CultureInfo | |
namespace WpfMutuallyExclusiveCheckableMenuItemsSample.Converters | |
{ | |
public class EnumToCheckableItemsConverter : IValueConverter | |
{ | |
public object Convert( | |
object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return value.ToString() == (string)parameter; | |
} | |
public object ConvertBack( | |
object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return Enum.Parse(targetType, (string)parameter); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfMutuallyExclusiveCheckableMenuItemsSample.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:WpfMutuallyExclusiveCheckableMenuItemsSample" | |
mc:Ignorable="d" | |
xmlns:vm="clr-namespace:WpfMutuallyExclusiveCheckableMenuItemsSample.ViewModels" | |
xmlns:cnv="clr-namespace:WpfMutuallyExclusiveCheckableMenuItemsSample.Converters" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<vm:MainViewModel /> | |
</Window.DataContext> | |
<Window.Resources> | |
<cnv:EnumToCheckableItemsConverter | |
x:Key="EnumToCheckableItemsConverter"/> | |
</Window.Resources> | |
<Grid> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition /> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition /> | |
</Grid.RowDefinitions> | |
<Menu Grid.Row="0" Grid.Column="0"> | |
<MenuItem Header="オプション(_O)"> | |
<MenuItem Header="Option A" IsCheckable="true" | |
IsChecked="{Binding Path=Options, | |
Converter={StaticResource EnumToCheckableItemsConverter}, | |
ConverterParameter=A, | |
UpdateSourceTrigger=PropertyChanged}" /> | |
<MenuItem Header="Option B" IsCheckable="true" | |
IsChecked="{Binding Path=Options, | |
Converter={StaticResource EnumToCheckableItemsConverter}, | |
ConverterParameter=B, | |
UpdateSourceTrigger=PropertyChanged}" /> | |
<MenuItem Header="Option C" IsCheckable="true" | |
IsChecked="{Binding Path=Options, | |
Converter={StaticResource EnumToCheckableItemsConverter}, | |
ConverterParameter=C, | |
UpdateSourceTrigger=PropertyChanged}" /> | |
</MenuItem> | |
</Menu> | |
<TextBox Grid.Row="1" Grid.Column="0" | |
Text="{Binding Path=TextContent, UpdateSourceTrigger=PropertyChanged}" /> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
namespace WpfMutuallyExclusiveCheckableMenuItemsSample | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WpfMutuallyExclusiveCheckableMenuItemsSample.Models | |
{ | |
public enum Options { A, B, C, } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; // INotifyPropertyChanged | |
using WpfMutuallyExclusiveCheckableMenuItemsSample.Models; | |
namespace WpfMutuallyExclusiveCheckableMenuItemsSample.ViewModels | |
{ | |
public class MainViewModel : INotifyPropertyChanged | |
{ | |
// --------------------------------------------------------------------- | |
// プロパティ | |
private string _textContent; | |
public string TextContent | |
{ | |
get { return _textContent; } | |
set | |
{ | |
_textContent = value; | |
this.OnPropertyChanged(nameof(TextContent)); | |
} | |
} | |
private Options _options; | |
public Options Options | |
{ | |
get { return _options; } | |
set | |
{ | |
_options = value; | |
this.OnPropertyChanged(nameof(Options)); | |
switch (_options) | |
{ | |
case Options.A: | |
this.TextContent = "オプション A を選択"; | |
break; | |
case Options.B: | |
this.TextContent = "オプション B を選択"; | |
break; | |
case Options.C: | |
this.TextContent = "オプション C を選択"; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
// --------------------------------------------------------------------- | |
// コンストラクタ | |
public MainViewModel() | |
{ | |
// 初期設定 | |
this.Options = Options.A; | |
} | |
// --------------------------------------------------------------------- | |
// インターフェースの実装 - INotifyPropertyChanged | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void OnPropertyChanged(string name) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment