1

wpf Canvasクラスから派生した 'MyCanvas'というカスタムクラスがあります。 MyCanvasには、キャンバスのスケール変換を指定するDependency Property 'Scale'があります。 Scaleの値が変わると、変換を古い値から新しい値にアニメートしたいと思っています。そのため私はLayoutTransform.BeginAnimation(...)メソッドを使用しています。ScaleXとScaleYのLayoutTransformアニメーションはバインディングを中断しています

コード:

//This represents the time it will take to zoom 
Duration zoomDuration = new Duration(TimeSpan.Parse("0:0:0.3")); 

//Set up animation for zooming 
DoubleAnimationUsingKeyFrames scaleAnimation = new DoubleAnimationUsingKeyFrames(); 
scaleAnimation.Duration = zoomDuration; 
scaleAnimation.KeyFrames = GetAnimationSplines(newScale); 
scaleAnimation.Completed += new EventHandler(
    (sender, e) => Scale = newScale); 

// Start the scale (zoom) animations 
LayoutTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation); 
LayoutTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation); 

のXmaI:

<Grid> 
    <ItemsControl x:Name="Items"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <local:MyCanvas Scale="{Binding Scale, Mode=TwoWay}"> 
        <local:MyCanvas.LayoutTransform UseLayoutRounding="True"> 
         <ScaleTransform ScaleX="{Binding Scale, Mode=TwoWay}" 
             ScaleY="{Binding Scale, Mode=TwoWay}"/> 
        </local:MyCanvas.LayoutTransform> 
       </local:MyCanvas> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</Grid> 

しかしScale(のviewmodelでプロパティ)とScaleXScaleYの結合、このコードを実行した後

Scaleの値を変更すること、すなわち破壊されませんキャンバスの縮尺を変更しない。 (スヌープを使用して)

エラーメッセージ:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Scale; DataItem=null; target element is 'ScaleTransform' (HashCode=796423); target property is 'ScaleX' (type 'Double')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Scale; DataItem=null; target element is 'ScaleTransform' (HashCode=796423); target property is 'ScaleY' (type 'Double')

いずれかが、このためのソリューションを持っている場合は私に知らせてください。ありがとう

+0

ScaleTransformをLayoutTransformプロパティにどのように割り当てましたか?そのコードも表示してください。それともあなたは? – Clemens

答えて

0

、今日、私はいくつかのことをしようとしていたし、突然問題が解決しました。どのように動作するのか分かりませんでした。私はコードに変更を加えました。以前に試したこともあります。

scaleAnimation.FillBehavior = FillBehavior.Stop; 

誰でもこれを私に説明できますか?ありがとう。

0

これは上記の問題の解決策ではありませんが、上記の問題を再現する作業サンプルWPF Applicationです。

XAML:

<Window x:Class="TestWpfApplication.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="auto" /> 
    </Grid.RowDefinitions> 

    <Grid Grid.Row="0"> 
     <Grid.LayoutTransform> 
      <ScaleTransform ScaleX="{Binding ElementName=zoomer, Path=Value}" ScaleY="{Binding ElementName=zoomer, Path=Value}" x:Name="scaleTx" /> 
     </Grid.LayoutTransform> 

     <Border Background="Aqua" BorderThickness="3" BorderBrush="Blue" Height="50" Width="50" /> 
    </Grid> 
    <StackPanel Orientation="Horizontal" Grid.Row="1"> 
     <Slider x:Name="zoomer" Width="200" Value="1" Minimum="0.1" Maximum="3" TickFrequency="0.1" IsSnapToTickEnabled="True" Margin="3"/> 
     <Button Content="Animate" Click="Button_Click" Margin="3"/> 
    </StackPanel> 
</Grid> 

コード:まあ

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 

     //This represents the time it will take to zoom 
     Duration zoomDuration = new Duration(TimeSpan.Parse("0:0:0.5")); 

     //Set up animation for zooming 
     DoubleAnimationUsingKeyFrames scaleAnimation = new DoubleAnimationUsingKeyFrames(); 
     scaleAnimation.Duration = zoomDuration; 
     scaleAnimation.KeyFrames = GetAnimationSplines(zoomer.Maximum); 

     scaleTx.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation); 
     scaleTx.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation); 
    } 

    /// <summary> 
    /// This creates a spline for zooming with non-linear acceleration patterns. 
    /// </summary> 
    /// <param name="moveTo"></param> 
    /// <returns></returns> 
    protected DoubleKeyFrameCollection GetAnimationSplines(double moveTo) 
    { 
     DoubleKeyFrameCollection returnCollection = new DoubleKeyFrameCollection(); 

     returnCollection.Add(new LinearDoubleKeyFrame(moveTo, KeyTime.FromPercent(1.0))); 

     return returnCollection; 
    } 
} 

The shape in the center of window will scale when slide is moved. Once you click animate button to apply animation, the zooming will animate but after that slider stops working.

0

私はこれも起こったと確信できますが、私はまだきれいな解決策を見つけていません。アニメーションが実行された後にスライダの変更がバインドされたオブジェクトに影響を与えないことについて、あなたの答えの1つで説明するようなケースがありました(そして、FillBehaviorStopに設定されていましたか、BeginAnimation(someProperty, null)を実行してアニメーションプロパティを「保持」する。アニメーション後にプロパティを編集できることを示すために(ただし、バインディングの元であるスライダではない)、プロパティを特定の値に設定するボタンを設定します。実際にはアニメーションの後でそのプロパティを完全に変更することがありますが、WPFのバインドが特定のアニメーションの結果として破損することがあります。

コードでバインドを再確立することができますアニメーションのCompletedハンドラーで

関連する問題