If you try to use RadioButtons as usual in MVVM, it will not work as you expect.
For example, here is a very simple UI using two RadioButtons.

A very simple UI using two RadioButtons.


My first attempt is to use this View:
...
<GroupBox Header="I love">
    <StackPanel>
        <RadioButton IsChecked="{Binding IsPeaceLover}">Peace</RadioButton>
        <RadioButton>Earth</RadioButton>
    </StackPanel>
</GroupBox>
...
and this ViewModel:
class UserViewModel : ViewModelBase
{
    bool isPeaceLover = true;

    public bool IsPeaceLover
    {
        get { return this.isPeaceLover; }
        set
        {
            if (this.isPeaceLover == value)
                return;

            this.isPeaceLover = value;
            OnPropertyChanged("IsPeaceLover");
        }
    }
    ...
}
But as you may know, it doesn't work. There are many solutions to avoid this problem.
Anyway, I've created a new pattern that is more appropriate to MVVM (not using Code-Behind).
Try this View:
...
<GroupBox Header="I love">
    <StackPanel>
        <RadioButton IsChecked="{Binding IsPeaceLover}">Peace</RadioButton>
        <RadioButton IsChecked="{Binding IsEarthLover}">Earth</RadioButton>
    </StackPanel>
</GroupBox>
...
and this ViewModel:
class UserViewModel : ViewModelBase
{
    bool isPeaceLover = true;

    public bool IsPeaceLover
    {
        get { return this.isPeaceLover; }
        set
        {
            if (this.isPeaceLover == value)
                return;

            this.isPeaceLover = value;
            OnPropertyChanged("IsPeaceLover", "IsEarthLover");
        }
    }

    public bool IsEarthLover
    {
        get { return !IsPeaceLover; }
        set { IsPeaceLover = !value; }
    }
    ...
}
If you have more options user can choose, you can extend the pattern like this:
public enum Priority
{
    Low,
    Middle,
    High,
}

class JobViewModel : ViewModelBase
{
    Priority priority = Priority.Low;

    public Priority Priority
    {
        get { return this.priority; }
        set
        {
            if (this.priority == value)
                return;

            this.priority = value;
            OnPropertyChanged("Priority"
                     , "IsLowPriority", "IsMiddlePriority", "IsHighPriority");
        }
    }

    public bool IsLowPriority
    {
        get { return Priority == Priority.Low; }
        set { Priority = value ? Priority.Low : Priority; }
    }

    public bool IsMiddlePriority
    {
        get { return Priority == Priority.Middle; }
        set { Priority = value ? Priority.Middle : Priority; }
    }

    public bool IsHighPriority
    {
        get { return Priority == Priority.High; }
        set { Priority = value ? Priority.High : Priority; }
    }
}
...
<GroupBox Header="Priority">
    <StackPanel>
        <RadioButton IsChecked="{Binding IsLowPriority}">Low</RadioButton>
        <RadioButton IsChecked="{Binding IsMiddlePriority}">Middle</RadioButton>
        <RadioButton IsChecked="{Binding IsHighPriority}">High</RadioButton>
    </StackPanel>
</GroupBox>
...


신고

비주얼 스튜디오 2008 SP1에서 WPF 개발을 하다보면 다음과 같은 경고가 발생할 수 있습니다.

The type 'ConflictTest.MainWindow' in 'C:\ConflictTest\ConflictTest\MainWindow.xaml.cs' conflicts with the imported type 'ConflictTest.MainWindow' in 'c:\ConflictTest\ConflictTest\bin\Debug\ConflictTest.exe'.
Using the type defined in 'C:\ConflictTest\ConflictTest\MainWindow.xaml.cs'.

이 경고는 가끔씩 멈추는 WPF 디자이너 때문에 비주얼 스튜디오 프로세스를 강제 종료하면 발생될 수 있습니다.
이 문제는 비주얼 스튜디오가 알 수 없는 이유로 추가한 실행 파일에 대한 참조를 참조 목록에서 지우면 해결됩니다.

실행 파일에 대한 참조를 참조 목록에서 지우세요.


신고

You may encounter strange warnings like the following if you develop WPF applications in Visual Studio 2008 SP1.

The type 'ConflictTest.MainWindow' in 'C:\ConflictTest\ConflictTest\MainWindow.xaml.cs' conflicts with the imported type 'ConflictTest.MainWindow' in 'c:\ConflictTest\ConflictTest\bin\Debug\ConflictTest.exe'.
Using the type defined in 'C:\ConflictTest\ConflictTest\MainWindow.xaml.cs'.

These warnings can be happened when you end a Visual Studio Process of which WPF designer is hanging.
To solve this problem, you have to remove a self-reference the Visual Studio adds by some reason.

Remove the executable assembly in the reference list.


신고

티스토리 툴바