2011-07-15 15 views
1

私はWrapPanelにいくつかのアイテムを持っています。アイテムをクリックして折り返しパネルの全幅に広げたいと思っています。私は、各項目に使用されているコントロールでExpandedとColapsedの2つの状態を作成してこれを試みました。 Expanded状態では、WrapPanelのActualWidthと等しいコントロールの幅をバインドしました。状態変更でWPFバインディングが機能しないのはなぜですか?

私が期待した結果が得られなかったとき、Expanded値を(バインディングの代わりに)特定の数値に設定しようとしました。それは働いている。アイテムは、2つの折りたたまれた幅と誇張された幅を切り替えます。私はまだ、Expanded状態をWrapPanelの幅と同じにしたいのですが、任意の固定幅ではありません。ビジュアルステートではなく直接Widthプロパティを直接バインドすると、WrapPanelのアイテムの幅が一致するため、バインディングが機能することがわかります。

バインディングと拡張状態 - 動作しません:ハードコードされた値を持つ

<VisualState x:Name="Expanded"> 
<Storyboard> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="JobMaster"> 
     <EasingDoubleKeyFrame KeyTime="0"> 
      <EasingDoubleKeyFrame.Value> 
       <Binding 
        Path="ActualWidth" 
        RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type WrapPanel}}" /> 
      </EasingDoubleKeyFrame.Value> 
     </EasingDoubleKeyFrame> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

拡張した状態 - 作品

<VisualState x:Name="Expanded"> 
<Storyboard> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="JobMaster"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="800" /> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

バインドコントロールの幅プロパティを直接作品

<UserControl.Width> 
     <Binding 
     Path="ActualWidth" 
     RelativeSource="{RelativeSource AncestorType={x:Type WrapPanel}}" /> 
</UserControl.Width> 

国家のバインディングはなぜ機能しないのですか、これを行う別の方法はありますか?

答えて

1

視覚的な状態を使用してこれを行うことは決してできませんでした。代わりに私は振る舞いを書いた。

public class TileExpandColapseBehavoir : Behavior<Control> 
{ 
    private ITile _data; 

    #region Properties 

    public static readonly DependencyProperty TileControlProperty = DependencyProperty.Register("TileControl", typeof(object), typeof(TileExpandColapseBehavoir), new PropertyMetadata(null)); 
    public static readonly DependencyProperty DefaultWidthProperty = DependencyProperty.Register("DefaultWidth", typeof(Double), typeof(TileExpandColapseBehavoir), new PropertyMetadata(null)); 

    public object TileControl 
    { 
     get { return (object)this.GetValue(TileControlProperty); } 
     set { this.SetValue(TileControlProperty, value); } 
    } 

    public double DefaultWidth 
    { 
     get { return (double)this.GetValue(DefaultWidthProperty); } 
     set { this.SetValue(DefaultWidthProperty, value); } 
    } 

    #endregion 

    public TileExpandColapseBehavoir() 
    { 
    } 

    protected override void OnAttached() 
    { 
     this.AssociatedObject.PreviewMouseDown +=new MouseButtonEventHandler(AssociatedObject_MouseUp); 
    } 

    private void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     UIElement child = (UIElement)sender; 
     WrapPanel parentWrap = FindAncestorUtil.TryFindAcestor<WrapPanel>(child); 
     if (parentWrap != null && TileControl is UserControl) 
     { 
      GetData(); 
      if (_data.IsExpanded == false) 
      { 
       Binding newBinding = new Binding(); 
       newBinding.Source = parentWrap; 
       newBinding.Path = new PropertyPath("ActualWidth"); 

       UserControl thisTile = (UserControl)TileControl; 
       BindingOperations.SetBinding(thisTile, UserControl.WidthProperty, newBinding); 
       _data.IsExpanded = true; 
      } 
      else 
      { 
       UserControl thisTile = (UserControl)TileControl; 

       BindingOperations.ClearBinding(thisTile, UserControl.WidthProperty); 
       thisTile.Width = DefaultWidth; 
       _data.IsExpanded = false; 
      } 
     } 
    } 

    private void GetData() 
    { 
     if (_data == null && AssociatedObject.DataContext is ITile) 
     { 
      _data = (ITile)AssociatedObject.DataContext; 
     } 
    } 
} 
0

あなたのRelativeSourceバインディングは、アニメーションのターゲットではなく、アニメーションの祖先を探しています。あなたのWrapPanelに名前を付けて、代わりに要素の束縛を使用してみてください。

<Binding Path="ActualWidth" ElementName="MyWrapPanel"/> 
+0

応答ありがとうございました。私は試してみましたが、効果がないようです。 – PeaceFrog

+0

申し訳ありませんが、Visual State Managerで多くの経験がありません。ストーリーボードがスタイルトリガーで定義されている場合にのみ、私の提案が働くと思います。しかし、アニメーションをトリガするには、 "IsExpanded" _property_が必要です。 [プロパティ値が変更されたときにアニメーションをトリガする方法(http://msdn.microsoft.com/en-us/library/ms742536.aspx)VSMにそれを結びつける方法がわかりません。 –

関連する問題