2016-06-01 11 views
0

WP8.1アプリケーションでIValueConverterを使用してRadioButtonにプロパティをバインドする際に問題があります。 ここに私のコードです。メインページでIValueConverterを使用してRadioButtonをページプロパティにバインドします。

public sealed partial class MainPage : Page 
{ 
    private ObservableDictionary defaultViewModel = new ObservableDictionary(); 
    public ObservableDictionary DefaultViewModel 
    { 
     get { return this.defaultViewModel; } 
    } 

    private int radioValue; 
    public int RadioValue 
    { 
     get { return radioValue; } 
     set 
     { 
      if (value != -1) 
      { 
       radioValue = value; 
       /*some important action*/ 
      } 
     } 
    } 
... 
public MainPage() 
{ 
    this.InitializeComponent(); 
    ... 
    RadioValue = 1; 
    this.DefaultViewModel["RadioValue"] = RadioValue; 

} 

が、その後RadioValueConverter.csファイルに:XAMLで最後に

class RadioValueConverter : IValueConverter 
{ 
    public Object Convert(Object value, Type targetType, Object parameter, string language) 
    { 
     if (value == null || parameter == null) 
     { 
      return false; 
     } 
     if (value is int) 
      return Int32.Parse((string)parameter) == (int)value; 
     else return parameter.Equals(value); 
    } 

    public Object ConvertBack(Object value, Type targetType, Object parameter, string language) 
    { 
     if (parameter == null || (bool)value == false) 
     { 
      return -1; 
     } 
     if (parameter is string) 
      return Int32.Parse((string)parameter); 
     else return parameter; 
    } 
} 

... 
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Mode=Self}}" 

... 

<Page.Resources> 
    <ResourceDictionary> 
     <local:RadioValueConverter x:Key="RadioValueConverter"/> 
    </ResourceDictionary> 
</Page.Resources> 

... 

<RadioButton 
    GroupName="RadioValue" 
    IsChecked="{Binding RadioValue, ConverterParameter=1, Converter={StaticResource RadioValueConverter}, Mode=TwoWay}" 
      > 

問題はRadioValueセットメソッドが呼び出されることはありませんということですそれは値を変更せず、必要なタスクを実行しません。 誰かが自分のコードで何が間違っているか教えていただけますか? ありがとうございます。

答えて

0

DataContextObservableDictionaryに設定しています。 RadioValueプロパティにバインドしようとすると、WPFリフレクションはDefaultViewModel["RadioValue"]の代わりにDefaultViewModel.RadioValueを検索します。 DataContext

は、ObservableDictionaryにはありません。明らかに、WPFリフレクションは、MainPage.RadioValueを検索しません。 DataContextを設定するには

/// <summary> 
/// A class that you can set as your DataContext 
/// </summary> 
public class YourViewModel : INotifyPropertyChanged 
{ 
    private int _radioValue; 
    /// <summary> 
    /// The RadioValue that your binding will find 
    /// </summary> 
    public int RadioValue 
    { 
     get { return _radioValue; } 
     set 
     { 
     if (value != -1) { 
      _radioValue = value; 
      /*some important action*/ 
      OnPropertyChanged(); 
     } 
     } 
    } 

    /// <summary> 
    /// Implementation of INotifyPropertyChanged event 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Implementation of INotifyPropertyChanged 
    /// </summary> 
    /// <param name="propertyName">The name of the property that has changed</param> 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

編集:あなたのようなDataContext

何かとしてそれを使用したい場合は

独自のクラスにObservableDictionaryを交換する必要がありますあなた自身のクラスに、あなたはConstructorMainPageでそれをすることができます:
this.DataContext=new YourViewModel();

関連する問題