2013-02-11 100 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

相关问题