2016-12-06 9 views
1

私はRadioButtonのスイッチングをブロックする可能性を探していますが、まだClickイベントをキャッチします。 Unfortunatelly Enabled=falseまたはIsHitTestVisible=falseプロパティを使用してClickイベントを防止します。ラジオボタンの変更を延期

私が達成したいことは次のとおりです。
1.ユーザーはRadioButtonをクリックします。
2. Clickイベントから、引数としてハンドラを渡しても何らかのメソッドが呼び出されますが、アクティブなRadioButtonはまだ変更されていません。
3.ハンドラが呼び出されたとき、結果に応じてRadioButtonを切り替えるかどうかを指定します。

+0

デリゲートをパラメータとして使用したいと思いますか?そして、彼らは(代理人たち)どこでプライベートコレクションやTagプロパティ、あるいは各RadioButtonのメタ情報のどこに保管していますか? – EgoPingvina

+0

より正確には、私はC++ \ CLIメソッドを引数delegateとして呼び出します。私はC#クラスから引数メソッドとして渡します。 –

答えて

1

私はあなたのシンプルなために作成例。

NuGetPrismパッケージを忘れないでください。

私は3つのRadioButtonを作成し、いくつかのViewModelからFunc<bool>に設定します。 PreviewMouseDownイベントの発生後、TagプロパティからFunc<bool>という現在のデリゲートを呼び出します。

のViewModel:ビュー(デザイナー)の

namespace PostponeRadioButtonChange.Model 
{ 
    using System; 
    using System.Collections.Generic; 

    using Microsoft.Practices.Prism.Mvvm; 

    public class MainWindow : BindableBase 
    { 
     private List<Func<bool>> rbHandlers; 

     private string comment; 

     public List<Func<bool>> RbHandlers 
     { 
      get { return this.rbHandlers; } 
      private set { this.SetProperty(ref this.rbHandlers, value); } 
     } 

     public string Comment 
     { 
      get { return this.comment; } 
      set { this.SetProperty(ref this.comment, value); } 
     } 

     public MainWindow() 
     { 
      this.RbHandlers = new List<Func<bool>> 
      { 
       () => 
        { 
         this.Comment = "First RadioButton clicked"; 
         return false; // Here must be your condition for checking 
        }, 
       () => 
        { 
         this.Comment = "Second RadioButton clicked"; 
         return false; 
        }, 
       () => 
        { 
         this.Comment = "Third RadioButton clicked"; 
         return true; // For example, third not checked after click 
        } 
      }; 
     } 
    } 
} 

コンテンツ。

<StackPanel> 

    <TextBox Text="{Binding Path=Comment, Mode=OneWay}"/> 

    <RadioButton Content="First" 
       PreviewMouseDown="RadioButtonMouseDown" 
       Tag="{Binding Path=RbHandlers[0], Mode=OneTime}"/> 

    <RadioButton Content="Second" 
       PreviewMouseDown="RadioButtonMouseDown" 
       Tag="{Binding Path=RbHandlers[1], Mode=OneTime}"/> 

    <RadioButton Content="Third" 
       PreviewMouseDown="RadioButtonMouseDown" 
       Tag="{Binding Path=RbHandlers[2], Mode=OneTime}"/> 

</StackPanel> 

ビュー(コードビハインド):

namespace PostponeRadioButtonChange 
{ 
    using System; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Input; 

    using VM = PostponeRadioButtonChange.Model; 

    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new VM.MainWindow(); 
     } 

     private void RadioButtonMouseDown(object sender, MouseButtonEventArgs e) 
     { 
      var rb = (sender as RadioButton); 

      if (rb == null) 
       throw new InvalidCastException("RadioButtonMouseDown only for RadioButton's"); 

      e.Handled = (rb.Tag as Func<bool>)?.Invoke() ?? false; 
     } 
    } 
} 

それは最終的な解決のためによくないですが、一例として、あなたを助ける必要があります。イベントハンドラの代わりにCommandをVMに作成することもできます。

+1

ありがとう、それは私が必要以上には、非常に教育的な特にラムダ配列です。私のイベントを 'Click'から' PreviewMouseDown'に変更した後、 'e.Handled = true 'を使ってイベントを行いました。私はすでにハンドラを持っていて、それらのラジオボタンを切り替えることですべてのロジックを書きました。 –

+0

私はあなたを助けることができてうれしいです) – EgoPingvina

2

ラジオボタンでMouseDownイベントを処理する必要があります。ラジオボタンをオンにして、ラジオボタンをオンにしてトンネリングしないようにしてください。

static void OnMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    // if some logic... 
    e.Handled = true; 
} 
+1

これは動作しません。 まず、wpf RadioButtonのマウスクリックイベントは、MouseDownやPreviewMouseDownのような名前です。 第2に、この場合、PreviewMouseDownを使用する必要があります。 In MouseDown e.Handled = trueは機能しません。 – EgoPingvina

1

このように、あなたがセッター内のコールを置くことができるバインディングを使用:

XAML

<RadioButton Content="radiobutton" IsChecked="{Binding TestRadio, Mode=TwoWay}"/> 

コード

private bool _testRadio; 
    public bool TestRadio 
    { 
     get { return _testRadio; } 
     set { value = testradiohandler(); SetProperty(ref _testRadio, value); } 
    } 
    private bool testradiohandler() 
    { 
     return new Random().NextDouble() >= 0.5; 
    } 
関連する問題