我有一個自定義類,說'MyCanvas'派生自wpf Canvas類。 MyCanvas具有指定畫布的比例變換的依賴屬性「比例」。現在當縮放比例的值發生變化時,我想要將變換從舊值轉換爲新值。因爲我正在使用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);
XMAL:
<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>
但是執行此代碼,的ScaleX
和ScaleY
與Scale
(在視圖模型的特性)的結合後是打破即改變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')
請讓我知道如果任何人有解決這一點。謝謝
您在哪裏爲ScaleTransform屬性分配了ScaleTransform?請顯示該代碼。或者你不是? – Clemens 2012-03-26 07:56:38