2014-03-12 20 views
0

我嘗試將MVVM用於PixelSense項目。我結合一些元素的ScatterView:停用ScatterView的自動SVI包裝

<s:ScatterView x:Name="MainScatterView" ItemTemplateSelector="{DynamicResource myDataTemplateSelector}" ItemsSource="{Binding Path=MainMenus}"/> 

而且我定義了一些的DataTemplates:

<DataTemplate x:Key="ActivityTemplate"> 
     <s:ScatterViewItem Loaded="ScatterViewItem_Loaded"> 
      <TextBlock Text="{Binding Path=Text}" /> 
     </s:ScatterViewItem> 
    </DataTemplate> 

    <DataTemplate x:Key="MainMenuTemplate"> 
     <s:ScatterViewItem Height="{Binding Path=Size, Mode=TwoWay}" Width="{Binding Path=Size, Mode=TwoWay}"> 
      <TextBlock/> 
     </s:ScatterViewItem> 
    </DataTemplate> 

正如你所看到的,我嘗試綁定(例如)高度屬性到視圖模型。

它不工作,因爲我的SVI(ScatterViewItem)會被另一個SVI自動包裝。這由ScatterView完成。我的問題是現在:我如何停用此功能,或者您知道解決方法?

THX幫助我;-)

回答

0

我發現了一個解決方法......這是不是最好的,但它的工作原理:)也許有人會有這樣的問題太多:

我刪除從周圍ScatterViewItem模板,並添加加載事件:

<DataTemplate x:Key="ActivityTemplate"> 
      <TextBlock Text="{Binding Path=Text}" Loaded="TextBlock_Loaded"/> 
    </DataTemplate> 

    <DataTemplate x:Key="MainMenuTemplate"> 
      <TextBlock Width="20" Height="20" Text="Hallo" Loaded="TextBlock_Loaded"/> 
    </DataTemplate> 

剩下的就是在後面的代碼:

private void TextBlock_Loaded(object sender, RoutedEventArgs e) 
    { 
     //Get the sourrounding ScatterViewItem via the VisualTree 
     System.Windows.Media.Visual parent = (System.Windows.Media.Visual)VisualTreeHelper.GetParent((System.Windows.Media.Visual)sender); 
     while (!(parent is ScatterViewItem)) 
     { 
      parent = (System.Windows.Media.Visual)VisualTreeHelper.GetParent((System.Windows.Media.Visual)parent); 
     } 

     //the current parent is the surrounding SVI 
     ScatterViewItem svi = parent as ScatterViewItem; 

     //Bind the properties to the SVI 
     Binding myBinding = new Binding("Size"); 
     myBinding.Source = svi.DataContext; 
     svi.SetBinding(ScatterViewItem.HeightProperty, myBinding); 
     svi.SetBinding(ScatterViewItem.WidthProperty, myBinding); 
    } 

如果你知道更好的解決方案:請讓我知道;)