2016-09-04 5 views
0

を結合私は、ツールバー、マスターズリストと詳細ビューでアプリケーションを次ていますマスター/ディテールツールバーコマンド

Overview

詳細をContentControlに介して、「注入さ」されます。 詳細には、ScrollViewerなどを含むUserControlが含まれています。ある時点で、「FitView」コマンドを提供する「ZoomPanControl」(私のものではない)があります。

現在アクティブな詳細ビューのツールバーから "FitView"コマンドを実行します。

私のツールバーのボタンは、次のようになります。

<fluent:Button x:Name="FitButton" Command="{Binding ?WhatGoesHere?}" Header="Fit View"/> 

私は、現在アクティブなZoomPanControlにツールバーのボタンのコマンドプロパティをバインドする方法を見つけ出すことはできません。私はコマンドバインディングを実行するときにこのコントロールを "見る"ことさえしません。

この問題がどのように解決されるかについてのヒントは高く評価されます。

答えて

0

ここで私が問題を解決した方法です。幸い私はZoomPanControlのソースコードにアクセスできます。

私はこのような「FitView」コマンドのZoomPanControl中たDependencyPropertyを実装している最初の:私は依存プロパティを設定するコントロールの「OnApplyTemplate()」メソッドで

public static readonly DependencyProperty FitCommandDepPropProperty = DependencyProperty.Register(
     "FitCommandDepProp", typeof (ICommand), typeof (ZoomAndPanControl), new PropertyMetadata(default(ICommand))); 

    public ICommand FitCommandDepProp 
    { 
     get { return (ICommand) GetValue(FitCommandDepPropProperty); } 
     set { SetValue(FitCommandDepPropProperty, value); } 
    } 

FitCommandDepProp = FitCommand; 
自分のアプリケーションの詳細ビューで

私はこのように私のViewModelにコマンド依存関係プロパティをバインド:

<zoomAndPan:ZoomAndPanControl x:Name="zoomAndPanControl" 
             FitCommandDepProp="{Binding FitCommand, Mode=OneWayToSource}" 

重要な部分は、Mode = OneWayToSourceです。これは、ZoomPanControlのコマンドをdetail-viewmodelに「転送」します。

Detail-viewmodelには、ICommandにバインドするプロパティがあります。この時点から私は私のビューモデルロジックでコマンドを持っています。私はツールバーにバインドされているビューモデルにFitCommandを渡すためのメカニズムを実装しました。イベントを使用することもできますし、好きなようにコマンドを渡すこともできます。

ツールバーのビューモデルには、FitCommandのICommandプロパティもあります。私は、このプロパティにバインドするだけツールバー-ビューで

public ICommand FitCommand 
    { 
     get { return _fitCommand; } 
     set 
     { 
      if (Equals(value, _fitCommand)) return; 
      _fitCommand = value; 
      NotifyOfPropertyChange(() => FitCommand); 
     } 
    } 

:その後

<fluent:Button x:Name="FitButton" Command="{Binding FitCommand}" Header="Fit View"/> 

、ビュー・コマンドは、個別に各詳細ビューのために用意されています。

しかし、私はZoomPanControlのソースコードにアクセスすることなくこれを解決する方法は知られていません。

0
public static class WpfHelper 
{   
    public static IEnumerable<DependencyObject> GetVisualChildsRecursive(this DependencyObject parent) 
    { 
     if (parent == null) 
      throw new ArgumentNullException("parent"); 

     int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < numVisuals; i++) 
     { 
      var v = VisualTreeHelper.GetChild(parent, i); 

      yield return v; 

      foreach (var c in GetVisualChildsRecursive(v)) 
       yield return c; 
     } 
    } 
} 

//コマンド実行

this.DetailView.GetVisualChildsRecursive().OfType<ZoomPanControl>().First().FitView(); 

//コマンドCanExecute

this.DetailView.GetVisualChildsRecursive().OfType<ZoomPanControl>().Any();