私はあなたのシンプルなために作成例。
NuGet
Prism
パッケージを忘れないでください。
私は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に作成することもできます。
デリゲートをパラメータとして使用したいと思いますか?そして、彼らは(代理人たち)どこでプライベートコレクションやTagプロパティ、あるいは各RadioButtonのメタ情報のどこに保管していますか? – EgoPingvina
より正確には、私はC++ \ CLIメソッドを引数delegateとして呼び出します。私はC#クラスから引数メソッドとして渡します。 –