2010-08-16 36 views
0

我有一個Silverlight 4中的ItemControl,其中Canvas作爲ItemPanel,這個想法是模擬一個帶拖拽和dop項目的畫布區域。 ItemsControl有一個帶圖像和一個按鈕的ItemTemplate。如何在運行時更改itemControl中項目的itemTemplate silverlihgt 4

這個想法是,當itemTemplate的按鈕點擊itemTemplate改變。

我的一些代碼: (項目控制)

<ItemsControl ItemsSource="{Binding Devices}" 
       ItemTemplate="{StaticResource deviceItemTemplate}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
        MouseLeftButtonDown="Canvas_MouseLeftButtonDown" 
        MouseMove="Canvas_MouseMove" 
        LostMouseCapture="Canvas_LostMouseCapture"></Canvas> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 

(ItemTemplate中)

<DataTemplate x:Key="deviceItemTemplate"> 
    <ContentControl> 
     <Grid IsHitTestVisible="True" Background="Transparent"> 
      <Grid.RowDefinitions> 
       <RowDefinition></RowDefinition> 
       <RowDefinition Height="30"></RowDefinition> 
      </Grid.RowDefinitions> 
      <Image IsHitTestVisible="False" Grid.Row="0" Stretch="UniformToFill" 
        Width="50" Height="50" 
        Source="{Binding ImagePath}"/> 
      <Button Grid.Row="1" Content="{Binding EditarDispositivoCommand.DisplayName}" Command="{Binding EditarDispositivoCommand.Command}"></Button> 
     </Grid> 
     <ContentControl.RenderTransform> 
      <TranslateTransform X="{Binding X, Mode=TwoWay}" Y="{Binding Y, Mode=TwoWay}"></TranslateTransform> 
     </ContentControl.RenderTransform> 
    </ContentControl> 
</DataTemplate> 

我試圖讓,當在ItemTemplate中一個按鈕,點擊此項目變更的模板從資源到其他模板。

這是可能的或我採取了一個壞的方法。非常感謝。

回答

1

您可以嘗試使用Silverlight的DataTemplateSelector - 它不是像WPF一樣內置的,但它可以通過一些額外的代碼來實現。

這裏是從CodeProject一個很好的例子: http://www.codeproject.com/KB/silverlight/SLTemplateSelector.aspx

你查看模型,以指定模板只需添加元素...

+0

你好chadbr thatnks爲你的迴應,我嘗試你提供給我的解決方案,但問題是,使用DataTemplateSelector的ItemCollection使用它只有第一次加載,然後一次顯示的項目,我需要更改模板。例如,如果在codeproject的例子中,你告訴我每個城市都有一個按鈕,當你點擊按鈕這個城市的國家改變爲其他... – Diego 2010-09-23 00:32:01

相關問題