我有一個ItemsControl
顯示一堆矩形。每個矩形需要向上和向左偏移。所以,我創建了一個RectangleStyle
,它使用綁定爲矩形設置寬度,高度,X平移和Y平移。WPF風格的綁定導致莫名其妙的「無法找到治理FrameworkElement」錯誤
寬度和高度綁定工作正常,但我得到了TranslateTransform
綁定以下錯誤:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Offset.X; DataItem=null; target element is 'TranslateTransform' (HashCode=16452547); target property is 'X' (type 'Double')
這裏是我的ItemControl
的定義:
<ItemsControl
Style="{StaticResource ItemsControlStyle}"
ItemsSource="{Binding Zones}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Style="{StaticResource RectangleStyle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
這裏ItemsControlStyle
的定義:
<Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Point.X}" />
<Setter Property="Canvas.Top" Value="{Binding Point.Y}" />
</Style>
</Setter.Value>
</Setter>
</Style>
這裏是RectangleStyle
定義:
<Style x:Key="RectangleStyle" TargetType="Rectangle">
<Setter Property="Width" Value="{Binding Size.Width}" />
<Setter Property="Height" Value="{Binding Size.Height}" />
<Setter Property="RenderTransform">
<Setter.Value>
<!-- these bindings are causing the error -->
<TranslateTransform X="{Binding Offset.X}" Y="{Binding Offset.Y}" />
</Setter.Value>
</Setter>
</Style>
在RenderTransform
二傳手RectangleStyle
兩個綁定在錯誤的原因,但我不知道該怎麼做來解決這個問題。有趣的是,圖形被正確轉換,所以WPF能夠解析綁定 - 由於某種原因,它們並不滿意。
我能做些什麼來解決綁定問題?
編輯
我提交了一份關於MS連接一個bug報告:
好吧,它很容易複製。我嘗試了大約10種不同的東西,但綁定工作正常,我還想知道你是否應該在MS Connect上登錄這個,並讓他們想知道它是否是一個錯誤。 – 2010-07-06 23:03:39
看起來bug報告鏈接已更改或不再公開。想知道這個解決方案,因爲添加x:Name對我沒有任何幫助。 – 2015-09-28 18:15:38