2016-08-27 7 views
0

私は、この問題を遭遇したとき、普遍的なWindowsアプリケーションのいくつかのことを勉強していました。 コードなしのハンバーガーボタンでsplitviewメニューを構築したいと思います。 私はそのプロパティの値を変更するためのプロパティとコマンドでviewmodelをセットアップします。コマンドが発射されたかどうかを調べるために私は小さなメッセージを含んだ。 私はsplitview IsPaneOpenを私のviewmodelにバインドしましたが、何とか動作していないようです。C#UWP binding splitview IsPaneOpen to viewmodel

XAMLコード

<Page.Resources> 
    <vm:PaneViewModel x:Key="viewModel"/> 
</Page.Resources> 
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource viewModel}" > 
    <Button Content="Test" Command="{Binding Path=OpenPane, Mode=TwoWay}" ManipulationMode="All"/> 
    <SplitView DisplayMode="CompactInline" 
       x:Name="Splitview" 
       OpenPaneLength="150" 
       CompactPaneLength="20" 
       IsPaneOpen="{Binding IsPaneOpen, Mode=TwoWay}"> 
     <SplitView.Pane> 
      <StackPanel Height="400"> 
       <Button Height="40"> 
        <TextBlock Text="Testbutton" Width="100"/> 
       </Button> 
      </StackPanel> 
     </SplitView.Pane> 
     <SplitView.Content> 
      <TextBlock Margin="30" Text="{Binding ShowAnything, Mode=TwoWay}"/> 
     </SplitView.Content> 
    </SplitView> 
</StackPanel> 

のViewModelコード

internal class PaneViewModel 
{ 
    public PaneViewModel() 
    { 
     OpenPane = new OpenPaneCommand(this); 
    } 
    private bool isPaneOpen = true; 
    public bool IsPaneOpen 
    { 
     get 
     { 
      return isPaneOpen; 
     } 

     set 
     { 
      isPaneOpen = value; 
     } 
    } 
    public string ShowAnything { get { return isPaneOpen.ToString(); } } 
    public OpenPaneCommand OpenPane { get; set; } 

    public void OpenPaneMethod() 
    { 
     if (isPaneOpen == false) 
      isPaneOpen = true; 
     else 
      isPaneOpen = false; 
    } 
} 

とコマンドコード

internal class OpenPaneCommand : ICommand 
{ 
    public OpenPaneCommand(PaneViewModel ViewModel) 
    { 
     this.viewModel = ViewModel; 
    } 
    private PaneViewModel viewModel; 

    public event EventHandler CanExecuteChanged; 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public void Execute(object parameter) 
    { 
     Blub(); 
     viewModel.OpenPaneMethod(); 
    } 
    async private void Blub() 
    { 
     var dialog = new MessageDialog("Testausgabe"); 
     await dialog.ShowAsync(); 
    } 

私が言ったように - messagedialogが現れますが、splitview.content内のTextBlockでもありませんまたはispaneopenが変化するようです。デバッグは、値を変更する私の方法が実際に値を変更することを私に示しています。 私は私のバインディングまたはdatacontext設定がオフになっていたのかしらと思いました。

多分、あなたは私の勘違いがどこから来るのかを知っているかもしれません。

ありがとうございました!私はあなたのビューモデルがObservableObjectを継承するべきだと思い

ミリ当

答えて

0

。 さらに、すべてのプロパティは、以下のように更新する必要があります:

public bool IsPaneOpen 
     { 
      get { return isPaneOpen; } 
      set 
      { 
       this.Set<bool>(ref this._loisPaneOpendedPendingCount, value); 
      } 
     } 

同じプロパティ

+0

ObservableObjectがINotifyPropertyChangedの特定の実装である、http://stackoverflow.com/questions/10093180/observableobject-or-inotifypropertychanged-onを参照してください-viewmodels – Bart

1

ShowAnythingのためのあなたのビューは、あなたのViewModel上の変更を通知されていないため、動作していない結合。お使いのVMはINotifyPropertyChangedインターフェイスを実装し、プロパティが変更されるたびにPropertyChangedイベントを発生させる必要があります。

だから、一般的に、あなたのセッターにこのようなものを置く:

... 
set 
{ 
    isPaneOpen = value; 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsPaneOpen"); 
} 
... 
関連する問題