我正在使用Windows 8應用程序。我必須在畫布內放置幾個控件(圖像,矩形)。如果我直接使用它,當我在孩子上使用「Canvas.Left」附加屬性(上述控件)時,一切正常。不過,我想使用MVVM。因此,我現在使用像這樣的ItemsControl:爲ItemsControl項目設置Canvas.Left
<ItemsControl ItemsSource="{Binding MyObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Image}" Visibility="{Binding IsImage, Converter={StaticResource boolConverter}}" />
<Rectangle Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding Fill}" Visibility="{Binding IsNoImage, Converter={StaticResource boolConverter}}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
除了附加的Canvas屬性,綁定正在工作。經過一番研究後,我發現這是因爲ItemsControl用ContentPresenter封裝了它的子節點。因此,我試圖讓使用解決方案,我就發現了計算器,但沒有成功:
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Left}" />
</Style>
</ItemsControl.ItemContainerStyle>
添加該代碼沒有設置Canvas.Left屬性(甚至去除的DataTemplate中綁定後)。
我錯過了什麼?提前致謝。
可能與http://stackoverflow.com/questions/11857505/how-do-i-do-bindings-in-itemcontainerstyle-in-winrt有關 – onelaview