2016-05-05 6 views
0

私は2つの異なるViewModelクラスに公開するModelクラスに静的なboolプロパティを持っています。これらのViewModelの1つは、前記静的プロパティにリンクされたboolプロパティを有し、コンバータを介してボタンの可視性に拘束される。 ViewModel内でこれをtrueまたはfalseに設定すると、ボタンの可視性がそれに応じて変更されます。 (このViewModelのインスタンスは、ボタンが存在するDataContext経由でビューのXAMLに設定されています) 別のビュー内からこのボタンの可視性を変更したいと思っていました。別のViewのViewModelも元のモデルの静的ブールにリンクされていますが、これを行うことはできますが、何もしていません。バインディング時の静的フィールドの問題

ここに私のコードです:

MainModel

public class MainModel 
{ 
    static bool _ButtonIsVisible = true; 

    public static bool ButtonIsVisible 
    { 
     get { return _ButtonIsVisible; } 
     set { _ButtonIsVisible = value; } 
    } 
} 

MainViewModel

class MainViewModel: ObserveableObject 
{ 
    public bool ButtonIsVisible 
    { 
     get { return MainModel.ButtonIsVisible; } 
     set 
     { 
      MainModel.ButtonIsVisible = value; 
      RaisePropertyChanged("ButtonIsVisible"); 
     } 
    } 
} 

MAINVIEW

<Window x:Class="MVVM.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:local="clr-namespace:MVVM" 
    mc:Ignorable="d" 
    Title="MainWindow" Width="1920" Height="1080" WindowState="Maximized" WindowStyle="None"> 

    <Window.DataContext> 
     <local:MainViewModel/> 
    </Window.DataContext> 

    <Window.Resources> 
     <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/> 
    </Window.Resources> 

    <Button Visibility="{Binding ButtonIsVisible, Converter={StaticResource BoolToVisConverter}}" /> 
</Window> 

MainViewModelからのButtonIsVisibleがコマンド内で変更され、期待どおりに動作します。これは私のトラブルが起こる場所です。

AnotherViewModel

class AnotherViewModel: ObserveableObject 
{ 
    public bool ButtonIsVisible 
    { 
     get { return MainModel.ButtonIsVisible; } 
     set 
     { 
      MainModel.ButtonIsVisible = value; 
      RaisePropertyChanged("ButtonIsVisible"); 
     } 
    } 
} 

AnotherViewModelのインスタンスは、それがビューに対応していますし、コマンドは私がAnotherViewModelからButtonIsVisibleプロパティを変更するこのビュー内のボタンにバインドされ、時のDataContextのを介して作成されます私のMainViewからのボタンが、MainModelの静的プロパティから両方のViewModelが取得してプロパティの値を取得して設定しているのを見て、私のボタンがMainViewから変更されると思いますが、これは機能しません。 誰かが私が間違っていることを教えてもらえますか?

+0

これはなぜ機能していないのかわかりませんが、これを行うには静的なプロパティを使用して私には本当に悪いと思います。私は 'AnotherViewModel'から' MainViewModel'' ButtonIsVisible'プロパティを変更したいと思います。コンストラクタの 'AnotherViewModel'に' MainViewModel'インスタンスを送るかもしれません。 – Pikoh

+0

私はそれについて考えて、 'AnotherViewModel'では' RaisePropertyChanged'を呼びますが、 'MainView'ではなく' AnotherViewModel'のビューに影響します。 'MainView'はプロパティ変更イベントを取得していません... – Pikoh

+0

私は自分自身もこれを行うために静的プロパティを使用するのが嫌いです。私は後でより良い方法を見つけるかもしれませんが、私は今ボールを転がすためにこれで行くつもりです。 2番目のコメントは意味がありますが、これを回避する方法はありますか? 'AnotherViewModel'で行われた変更の' MainViewModel'を通知できますか? –

答えて

1

あなたのコメントを1として、あなたはこのような何か持っている:だから、あなたに必要なもの

ViewModel = new AnotherViewModel(); 

<ContentControl Content="{Binding ViewModel}" /> 

をそして、あなたはそれを表示したい場合、あなたはこれをやっていますAnotherViewModelは次のとおりです。

MainViewModel MVM; 

public AnotherViewModel(MainViewModel _mvm) 
{ 
    this.MVM=_mvm; 
} 

あなたは、その後に自分のAnotherVieModel Instantationを変更する必要があります:

ViewModel = new AnotherViewModel(this); 

そして、あなたは、ボタンの表示を変更したい場合、あなただけの、これが行う必要があります:

this.MVM.ButtonIsVisible=true; 

私はあなたに言ったように、これはちょうどそれを行う方法ですが、私は思いますかなり単純明快です。疑問がある場合は、お気軽にお問い合わせください。

+0

私はあなたがどこから来ているのかを見ています。それは実際には比較的単純で順調に見えますが、これは素晴らしいことです!しかし、あなたが言うとき、私は1つの質問があります: "AnotherViewModelインスタンシエーションを次のように変更する必要があります: ' ViewModel = new AnotherViewModel(this); '" 正確にどこに行くのですか?それは私のAnotherViewModelのインスタンス化は、対応するビューのxamlで行われます。 –

+0

まあ、私はいつも、コードの中でviemodelのインスタンス化を行います。私はあなたがXamlからそれを削除し、あなたのMainViewModelで 'AnotherView w = new AnotherView();のようなことをすることをお勧めします。 w.DataContext = new AnotherViewModel(これ);あなたのコードがどれくらい正確かわかりません。 – Pikoh

+0

私はできました 'SelectedViewModel = new AnotherViewModel(これ);'どのビューが動作するかを表示するにはしかし、 'this.MVM.ButtonIsVisible = true;を試してみると、MVMがオブジェクトのインスタンスに設定されていないことを伝えるNullReferenceExceptionが発生しますが、ビューを変更すると、MainViewModelに設定されていることがわかりますだから、それがなぜ私の外にあるのか? 途中で助けてくれてありがとう!私は間違いなく正しい方向に動いている、私は今これとほぼ同じだと感じている! –

関連する問題