namespace WpfRadioButton
{
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ViewModel vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = vm;
}
}
[ValueConversion(typeof(bool), typeof(Enum))]
public class EnumToBoolExtension : MarkupExtension, IValueConverter
{
#region IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return parameter.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool) value) == true ? parameter : DependencyProperty.UnsetValue;
}
#endregion
#region MarkupExtension
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#endregion
}
public enum Direction
{
North,
Easth,
West,
South
}
public class ViewModel : INotifyPropertyChanged
{
public Direction direction;
public Direction Direction
{
get
{
return this.direction;
}
set
{
this.direction = value;
this.RaisePropertyChanged("Direction");
}
}
#region NotifyPropertyChanged Methods
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
<Window x:Class="WpfRadioButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfRadioButton"
Title="RadioButton" Height="200" Width="300">
<Grid DataContext="{Binding}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<RadioButton Grid.Row="0" GroupName="directionText" Margin="5" VerticalAlignment="Center" Content="North"
IsChecked="{Binding Direction, Converter={local:EnumToBool}, ConverterParameter={x:Static local:Direction.North}}"/>
<RadioButton Grid.Row="1" GroupName="directionText" Margin="5" VerticalAlignment="Center" Content="East"
IsChecked="{Binding Direction, Converter={local:EnumToBool}, ConverterParameter={x:Static local:Direction.Easth}}"/>
<RadioButton Grid.Row="2" GroupName="directionText" Margin="5" VerticalAlignment="Center" Content="West"
IsChecked="{Binding Direction, Converter={local:EnumToBool}, ConverterParameter={x:Static local:Direction.West}}"/>
<RadioButton Grid.Row="3" GroupName="directionText" Margin="5" VerticalAlignment="Center" Content="South"
IsChecked="{Binding Direction, Converter={local:EnumToBool}, ConverterParameter={x:Static local:Direction.South}}"/>
<TextBlock Grid.Row="4" Margin="5" VerticalAlignment="Center" Text="{Binding Direction}"/>
</Grid>
</Window>
출처 : https://zamjad.wordpress.com/2014/03/01/radio-button-in-mvvm/
'wpf' 카테고리의 다른 글
listbox 스크롤 안될때 확인해야하는점 (0) | 2019.02.14 |
---|---|
리스트에 빈칸없이 꽉 차게 넣는 로직 알고리즘 (0) | 2019.01.22 |
스크롤시 프로그램창 자체가 움직일때 해결 (0) | 2018.10.23 |
WPF Title Icon 제거하기 (0) | 2018.08.31 |
WPF 강제 소프트웨어 렌더링 설정 (0) | 2018.08.29 |