2017-01-27 4 views
1

私はCvs WPF for mvvmで作業していますが、セレクタを作成するためにアプリケーションを設計する方法がわかりません。トグルボタンでセレクタを作成

私はポイントを含むクラスを持っている:

public class MyCustomRectangle 
{ 
    public MyCustomPoint Point1; 
    public MyCustomPoint Point2; 
    public MyCustomPoint Point3; 
    public MyCustomPoint Point4; 
} 

私のアプリケーションは、画像が含まれています。ユーザーは画像をクリックして4点を描くことができます。彼は描画するポイントを選択してクリックする必要があります。

enter image description here

私はMainViewModelにバインドさトグルボタンとしてセレクタをやってみたかった:

class MainViewModel : ViewModelBase 
{ 
    private MyCustomRectangle _myPoints; 
    public MyCustomRectangle MyPoints 
    { 
     get 
     { 
      return _myPoints; 
     } 
    } 

    public int _selectedPointIndex; 
    public int SelectedPointIndex 
    { 
     get 
     { 
      return _selectedPointIndex; 
     } 
     set 
     { 
      _selectedPointIndex = value; 
      OnPropertyChanged(); 
     } 
    } 

    public int SelectedPoint 
    { 
     get 
     { 
      return MyPoints.GetPoint(SelectedPointIndex); 
     } 
    } 

} 

しかし、私は簡単にビューモデルに私のボタンをバインドする方法を知りません。この動作は次のいずれかである必要があります。

  • すべてのボタンはチェックされていません。
  • ユーザチェックボタン1の場合、ボタン1がチェックに合格するとSelectedPointIndex = 1
  • ユーザチェックボタン2と、ボタン2がチェックに合格するとSelectedPointIndex = 2、ボタン1が
オフに

実は私は持っている:

<ToggleButton Grid.Row="0" Grid.Column="0" Content="Point 1"/> 
<ToggleButton Grid.Row="0" Grid.Column="1" Content="Point 2"/> 
<ToggleButton Grid.Row="1" Grid.Column="0" Content="Point 3"/> 
<ToggleButton Grid.Row="1" Grid.Column="1" Content="Point 4"/> 

どのように私は単に私のボタンをバインドすることができますか?

+0

<Style x:Key="RadioButtonLooksAsToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton"/> 

は、スタイルを使用しますか? –

+0

私はWPFの専門家ではないので、私はそれを実行する方法を知らない。 ToggleButtonは使いやすいようでした。 –

答えて

1

期待される動作にはRadioButtonsを使用し、ToggleButtonsのようなスタイルにしてください。 RadioButtonsを使用すると、ここで見つけることができます:https://www.wpftutorial.net/RadioButton.html

は、スタイルを定義します。あなたはラジオ・ボタンを使用し、それらのスタイルを設定しない理由

<RadioButton Style="{StaticResource RadioButtonLooksAsToggleButton}" /> 
+0

それは良いアイデアのようです。チェックされたラジオボタンを入手するにはどうすればいいですか?与えられたリンク内の –

+0

は、それがバウンドプロパティ 'CurrentOption'です。 –

関連する問題