2016-07-28 13 views
0

私はMainWindow.xaml Window.Resourcesセクションのアニメーションでストーリーボードを持っている:ローカルウィンドウリソースでプロパティを使用すると、プロパティへのバインディングが機能しないのはなぜですか?

<Window.Resources> 
    <Storyboard x:Key="ShowSearchGrid"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="SearchGrid"> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding ExpandedGridHeight}"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
</Window.Resources> 

財産ExpandedGridHeightがそうのようなカスタムクラスで定義されています。窓のデータコンテキスト

class ExpandedGridHeightClass : INotifyPropertyChanged 
{ 
    //The next bit handles the INotifyPropertyChanged implementation 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void Notify(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public Double ExpandedGridHeight 
    { 
     get { return _expandedGridHeight; } 
     set 
     { 
      if (value != _expandedGridHeight) 
      { 
       _expandedGridHeight = value; 
       Notify("ExpandedGridHeight"); 
      } 
     } 
    } 

    private Double _expandedGridHeight = 100; 
} 

メインウィンドウ()に設定されています。

ExpandedHeightのExpandedGridHeightプロパティの実際の値は、他の場所で設定され、DEPを変更している
ExpandedGridHeightClass ExpandedHeight; 

public MainWindow() 
{ 
    InitializeComponent(); 

    ExpandedHeight = new ExpandedGridHeightClass(); 
    this.DataContext = ExpandedHeight; 
} 

ウィンドウのサイズで終了します。私は、ウィンドウのタイトルのような様々な他の場所にプロパティをバインドしようとした

void showSearchGrid() 
{ 
    Storyboard ShowSearchGrid = this.FindResource("ShowSearchGrid") as Storyboard; 
    ShowSearchGrid.Begin(); 
} 

が、それは完全に働いた、と意図したとおりに更新:

ストーリーボードには、以下の機能から開始されます。ただし、コード例のようにローカルリソースのプロパティにバインドされていると、動作しません。アニメーションは通常どおりに開始されますが、ExpandedGridHeightにアニメートするのではなく、SearchGridは高さ値0にアニメーションされます。私はアニメーションがリソース内で定義されているが、この問題の修正を知らないので、それが疑わしい。どんな助けもありがとう! :)

+0

残念ながら、それとは運@Steve – Steve

+0

=パスを使用してみてください。 –

+0

これはバインディングには適していますか? '{バインディングDataContext.ExpandedGridHeight、RelativeSource = {RelativeSource AncestorType = Window}}'? (これは、 'ExpandedGridHeight'がウィンドウのviewmodelのメンバーであると仮定していますが、あなたが言うことから安全な仮定だと思います) –

答えて

0

私は答えとしてこれを置くこともできると思いました。この方法は間違いなく「ハック」なので、あなたの好みに合わないかもしれません。

ExpandedGridHeightClass:

class ExpandedGridHeightClass : INotifyPropertyChanged 
{ 
    private static ExpandedGridHeightClass _instance; 
    public static ExpandedGridHeightClass Instance 
    { 
     get 
     { 
      if (_instance == null) 
       _instance = new ExpandedGridHeightClass(); 
      return _instance; 
     } 
    } 

    /** 
    * Whatever your class already has 
    */ 
} 

XAML:

<EasingDoubleKeyFrame KeyTime="0:0:0.5" 
    Value="{Binding ExpandedGridHeight, Source={x:Static MyNamespace:ExpandedGridHeightClass.Instance}}"/> 
関連する問題