2013-02-11 99 views
0

我創建了一個按鈕,並寫下自己的行爲以清除分散的觀點,但它沒有工作:WPF Scatterview項目 - 如何通過單擊按鈕清除所有項目?

private void Button1_Click(object sender, RoutedEventArgs e) 
     { 
      DependencyObject parent = VisualTreeHelper.GetParent(this); 
      ScatterViewItem svi = null; 
      while (parent as ScatterView == null) 
      { 
       if (parent is ScatterViewItem) 
        svi = parent as ScatterViewItem; 
       parent = VisualTreeHelper.GetParent(parent); 
      } 

      ((ScatterView)parent).Items.Remove(svi);    
     } 

在此之前,我以爲可以重設這個代碼沒有工作的應用程序之一:(我添加使用系統.Diagnostics;)

private void Button1_Click(object sender, RoutedEventArgs e) 
    {  
     Process.Start(Application.ResourceAssembly.Location);  
     Application.Current.Shutdown();      
    } 

的XAML:

<s:SurfaceButton Content="Clear" Name="Button1" Click="Button1_Click" VerticalAlignment="Bottom" HorizontalAlignment="Center"/> 

你能告訴我什麼,我錯過了, 感謝

+0

你的問題的標題是「如何清除所有的項目......」,但你的代碼建議你實際上只是從ScatterView中刪除包含Button的特定ScatterViewItem。請更準確地說明你想達到的目標。查看聲明Button的XAML也會很有趣。我很確定所有VisualTree的東西都不是真的需要。 – Clemens 2013-02-11 10:16:23

+0

Ya沒錯。此代碼不起作用,我想要的。也許這是重置應用程序更好的主意。但它也沒有效果。我現在添加代碼。 – sgizm 2013-02-11 10:25:51

回答

0

你可以簡單地給ScatterView名稱

<s:ScatterView x:Name="scatterView" ... /> 

,然後從後面的代碼訪問它:

private void Button1_Click(object sender, RoutedEventArgs e) 
{ 
    scatterView.Items.Clear(); 
} 
+0

我完全是你說的,它的工作,謝謝! – sgizm 2013-02-11 12:38:32

相關問題