2017-02-22 1 views
1

J'utilise l'application C# WPF avec OxyPlotoxyPlot supprimer des points de coordonnées

Comment puis-je supprimer les coordonnées des points du nouveau système de coordonnées? Y a-t-il une commande "claire"? Je voudrais, par exemple, si je clique sur un bouton, les « cercles » disparaissent dans le système de coordonnées

Je l'ai essayé avec

Points.Clear(); 

Malheureusement, la mauvaise approche que vous avez la bonne approche pour moi , comme je peux supprimer mes coordonnées dans le système de coordonnées?

<Window x:Class="TestOxyPlot.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:oxy="http://oxyplot.org/wpf" 
     xmlns:local="clr-namespace:TestOxyPlot" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <oxy:Plot x:Name="oxyPlot" Title="{Binding Title}" Margin="207,53,0,0"> 
      <oxy:Plot.Axes> 
       <oxy:LinearAxis Position="Bottom" MinimumPadding="0.1" MaximumPadding="0.1"/> 
       <oxy:LinearAxis Position="Left" MinimumPadding="0.1" MaximumPadding="0.1"/> 
      </oxy:Plot.Axes> 

      <oxy:Plot.Series> 
       <oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None" MarkerType="Square" MarkerSize="5" MarkerFill="Black"/> 
      </oxy:Plot.Series> 

     </oxy:Plot> 
     <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="44,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" MouseLeave="textBox_MouseLeave" TextChanged="textBox_TextChanged"/> 
     <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="44,101,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged"/> 
     <Button x:Name="button" Content="Generate" HorizontalAlignment="Left" Margin="68,174,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/> 
     <Button x:Name="btClear" Content="Clear" HorizontalAlignment="Left" Margin="68,225,0,0" VerticalAlignment="Top" Width="75" Click="btClear_Click"/> 
     <Slider x:Name="slider" HorizontalAlignment="Left" Margin="17,10,0,0" VerticalAlignment="Top" Minimum="200" Maximum="400" ValueChanged="slider_ValueChanged" SmallChange="1" Width="400"/> 
     <Button x:Name="btBitmap" Content="Bitmap" HorizontalAlignment="Left" Margin="68,139,0,0" VerticalAlignment="Top" Width="75" Click="btBitmap_Click"/> 
    </Grid> 
</Window> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using OxyPlot; 

namespace TestOxyPlot 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      oxyPlot.Width = 200; 
      oxyPlot.Height = 200; 
      DataContext = this; 
      this.Title = "Example 2"; 
      double zufallszahlX; 
      double zufallszahlY; 
      // Zur Erstellung des Seeds 
      int h = DateTime.Now.Hour; 
      int m = DateTime.Now.Minute; 
      int s = DateTime.Now.Second; 
      String u = h.ToString() + m.ToString() + s.ToString(); 
      int iu = Int32.Parse(u); 
      Random zufall = new Random(iu); 

      zufallszahlX = zufall.NextDouble() * (10 - -10) + -10; 
      zufallszahlY = zufall.NextDouble() * (10 - -10) + -10; 
      this.Points = new List<DataPoint> 
       { 
            new DataPoint(zufallszahlX, zufallszahlY) 
           /* new DataPoint(10, 13), 
            new DataPoint(20, 15), 
            new DataPoint(30, 16), 
            new DataPoint(40, 12), 
            new DataPoint(50, 12), 
            new DataPoint(-50, -4.54541212) */ 
           }; 


     } 
     //public string Title { get; private set; } 

     public IList<DataPoint> Points { get; private set; } 

     private void textBox_MouseLeave(object sender, MouseEventArgs e) 
     { 


     } 

     private void textBox_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      try 
      { 
       oxyPlot.Width = Int32.Parse(textBox.Text); 
      } 
      catch (Exception error) 
      { 
       MessageBox.Show("Mistake: " + error); 
      } 

     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      double randomNumX; 
      double randomNumY; 
      int h = DateTime.Now.Hour; 
      int m = DateTime.Now.Minute; 
      int s = DateTime.Now.Second; 
      String u = h.ToString() + m.ToString() + s.ToString(); 
      int iu = Int32.Parse(u); 
      Random zufall = new Random(iu); 

      Points = new List<DataPoint>(); 
      for (int i = 0; i < 10; i++) 
      { 
       randomNumX = zufall.NextDouble() * (10 - -10) + -10; 
       randomNumY = zufall.NextDouble() * (10 - -10) + -10; 
       Points.Add(new DataPoint(randomNumX, randomNumY)); 
      } 
      Points.Add(new DataPoint(0, 0)); 
      Points.Add(new DataPoint(-10, -10)); 
      ls.ItemsSource = Points; 
     } 



     private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      try 
      { 
       oxyPlot.Height = Int32.Parse(textBox1.Text); 
      } 
      catch (Exception error) 
      { 
       MessageBox.Show("Mistake: " + error); 
      } 
     } 

     private void btClear_Click(object sender, RoutedEventArgs e) 
     { 
      Points.Clear(); 
     } 

     private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
     { 
      oxyPlot.Width = slider.Value; 
     } 

     private void btBitmap_Click(object sender, RoutedEventArgs e) 
     { 
      oxyPlot.ToBitmap(); 
     } 
    } 
} 

Répondre

2

Vous pouvez définir la ItemsSource être null:

private void btClear_Click(object sender, RoutedEventArgs e) 
{ 
    ls.ItemsSource = null; 
} 

Modifier

si vous ne voulez pas les valeurs du changement d'axe, vous peut définir les propriétés minimum et maximum:

private void btClear_Click(object sender, RoutedEventArgs e) 
{ 
    botAxis.Minimum = botAxis.InternalAxis.ActualMinimum; 
    botAxis.Maximum = botAxis.InternalAxis.ActualMaximum; 
    lefAxis.Minimum = lefAxis.InternalAxis.ActualMinimum; 
    lefAxis.Maximum = lefAxis.InternalAxis.ActualMaximum; 
    ls.ItemsSource = null; 
} 
+0

Merci pour la réponse. C'était la solution. – GabelUndMesser

0

Tout d'abord, ne pas mélanger DataBinding et CodeBehind-Operations.

Vous avez lié les points de manière correcte. Mais vous devez utiliser un ObservableCollection<T>

Le remplacement de ItemsSource peut générer un comportement inattendu dans votre application.

Voir le code ci-dessous sur la façon dont je suis arrivé que cela fonctionne:

public Window1() { 
    InitializeComponent(); 
    oxyPlot.Width = 200; 
    oxyPlot.Height = 200; 
    this.Title = "Example 2"; 
    double zufallszahlX; 
    double zufallszahlY; 
    // Zur Erstellung des Seeds 
    int h = DateTime.Now.Hour; 
    int m = DateTime.Now.Minute; 
    int s = DateTime.Now.Second; 
    string u = h + m.ToString() + s; 
    int iu = Int32.Parse(u); 
    var zufall = new Random(iu); 

    zufallszahlX = zufall.NextDouble() * (10 - -10) + -10; 
    zufallszahlY = zufall.NextDouble() * (10 - -10) + -10; 
    this.Points = new ObservableCollection<DataPoint> { new DataPoint(zufallszahlX, zufallszahlY) }; 
    this.DataContext = this; 
} 

public ObservableCollection<DataPoint> Points { 
    get; 
} 

private void textBox_MouseLeave(object sender, MouseEventArgs e) { 


} 

private void textBox_TextChanged(object sender, TextChangedEventArgs e) { 
    try { 
    oxyPlot.Width = Int32.Parse(textBox.Text); 
    } catch (Exception error) { 
    MessageBox.Show("Mistake: " + error); 
    } 

} 

private void button_Click(object sender, RoutedEventArgs e) { 
    double randomNumX; 
    double randomNumY; 
    int h = DateTime.Now.Hour; 
    int m = DateTime.Now.Minute; 
    int s = DateTime.Now.Second; 
    String u = h.ToString() + m.ToString() + s.ToString(); 
    int iu = Int32.Parse(u); 
    Random zufall = new Random(iu); 

    for (int i = 0; i < 10; i++) { 
    randomNumX = zufall.NextDouble() * (10 - -10) + -10; 
    randomNumY = zufall.NextDouble() * (10 - -10) + -10; 
    Points.Add(new DataPoint(randomNumX, randomNumY)); 
    } 
    Points.Add(new DataPoint(0, 0)); 
    Points.Add(new DataPoint(-10, -10)); 
} 



private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { 
    try { 
    oxyPlot.Height = Int32.Parse(textBox1.Text); 
    } catch (Exception error) { 
    MessageBox.Show("Mistake: " + error); 
    } 
} 

private void btClear_Click(object sender, RoutedEventArgs e) { 
    Points.Clear(); 
} 

private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { 
    oxyPlot.Width = slider.Value; 
} 

private void btBitmap_Click(object sender, RoutedEventArgs e) { 
    oxyPlot.ToBitmap(); 
} 

j'ai quitté le XAML comme il était.

Remarque

Alors que l'autre réponse pourrait fonctionner, il peut produire des problèmes si votre application est plus complexe

+0

Merci beaucoup – GabelUndMesser

+0

button_Click() ne fonctionne pas dans ce cas. Vous devez définir le ItemsSource explicitement. – Ron

+0

@Ramin uhm quoi? Le ItemsSource est une liaison par XAML ... Pas besoin de faire quoi que ce soit avec elle dans CodeBehind – lokusking