Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, there are three text box controls on my window, how can i use them to change values of FirstTeam, SecondTeam and ThiredTeam on pie chart?

XML
<Window x:Class="ChartTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" Loaded="Window_Loaded">
    <Grid>
        <chartingToolkit:Chart HorizontalAlignment="Left" Margin="12,0,0,64" Name="chart1" VerticalAlignment="Bottom" Width="345" Height="235" >
            <chartingToolkit:PieSeries ItemsSource="{Binding}"
                                       IndependentValueBinding="{Binding Path=Name}"
                                       DependentValueBinding="{Binding Path=Votes}"/>
        </chartingToolkit:Chart>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="435,43,0,0" Name="txtFirstTeam" VerticalAlignment="Top" Width="56"  />
        <TextBox Height="25" HorizontalAlignment="Left" Margin="435,69,0,0" Name="txtSecondTeam" VerticalAlignment="Top" Width="56"  />
        <TextBox Height="24" HorizontalAlignment="Left" Margin="436,103,0,0" Name="txtThiredTeam" VerticalAlignment="Top" Width="52"  />
    </Grid>
</Window>


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace ChartTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            List<Party> parties = new List<Party>();

            parties.Add(new Party() { Name = "FirstTeam", Votes = 10 });
            parties.Add(new Party() { Name = "SecondTeam", Votes = 30 });
            parties.Add(new Party() { Name = "ThiredTeam", Votes = 5 });

            chart1.DataContext = parties;
        }
    }
}
Posted
Updated 8-Jan-12 1:28am
v5

1 solution

There are several ways of doing it, but if you define the List of Party at class level, you'll be able to reference it's contents from any method. After that modify the values via text box event TextChanged. So something like:
C#
public partial class MainWindow : Window
    {
        List<Party> parties = new List<Party>();

        public MainWindow()
        {
            InitializeComponent();

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


            parties.Add(new Party() { Name = "FirstTeam", Votes = 10 });
            parties.Add(new Party() { Name = "SecondTeam", Votes = 30 });
            parties.Add(new Party() { Name = "ThiredTeam", Votes = 5 });

            chart1.DataContext = parties;
        }
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e) {
           ((Party)this.parties[0]).Votes = (int)textBox1.Text;
        }
    }

Of course proper data checks and so on must be written before the value of Votes is changed. Also since you're using the list as a datacontext, consider using ObservableCollection[^] instead.


Addition:

Consider the following example:
XAML
XML
<Window x:Class="ChartTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        Loaded="Window_Loaded">
    <Grid>
        <chartingToolkit:Chart HorizontalAlignment="Left" Margin="12,0,0,64" Name="chart1" VerticalAlignment="Bottom" Width="345" Height="235" >
            <chartingToolkit:PieSeries ItemsSource="{Binding}"
                                       IndependentValueBinding="{Binding Path=Name}"
                                       DependentValueBinding="{Binding Path=Votes}"/>
        </chartingToolkit:Chart>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="435,43,0,0" Name="txtFirstTeam" VerticalAlignment="Top" Width="56" TextChanged="txtFirstTeam_TextChanged" />
        <TextBox Height="25" HorizontalAlignment="Left" Margin="435,69,0,0" Name="txtSecondTeam" VerticalAlignment="Top" Width="56" TextChanged="txtSecondTeam_TextChanged" />
        <TextBox Height="24" HorizontalAlignment="Left" Margin="436,103,0,0" Name="txtThiredTeam" VerticalAlignment="Top" Width="52" TextChanged="txtThiredTeam_TextChanged" />
    </Grid>
</Window>

Code
C#
public partial class MainWindow : Window {
   System.Collections.ObjectModel.ObservableCollection<Party> parties = new System.Collections.ObjectModel.ObservableCollection<Party>();
   public MainWindow() {
      InitializeComponent();
   }
   private void Window_Loaded(object sender, RoutedEventArgs e) {

      parties.Add(new Party() { Name = "FirstTeam", Votes = 10 });
      parties.Add(new Party() { Name = "SecondTeam", Votes = 30 });
      parties.Add(new Party() { Name = "ThiredTeam", Votes = 5 });

      this.txtFirstTeam.Text = parties[0].Votes.ToString();
      this.txtSecondTeam.Text = parties[1].Votes.ToString();
      this.txtThiredTeam.Text = parties[2].Votes.ToString();

      chart1.DataContext = parties;
   }

   private void txtFirstTeam_TextChanged(object sender, TextChangedEventArgs e) {
      chart1.DataContext = null;
      this.parties[0].Votes = Int32.Parse(txtFirstTeam.Text);
      chart1.DataContext = parties;
   }

   private void txtSecondTeam_TextChanged(object sender, TextChangedEventArgs e) {
      chart1.DataContext = null;
      this.parties[1].Votes = Int32.Parse(txtSecondTeam.Text);
      chart1.DataContext = parties;
   }

   private void txtThiredTeam_TextChanged(object sender, TextChangedEventArgs e) {
      chart1.DataContext = null;
      this.parties[2].Votes = Int32.Parse(txtThiredTeam.Text);
      chart1.DataContext = parties;
   }

}
public class Party {
   public string Name { set; get; }
   public int Votes { set; get; }

}

That is just a sketch and the program will fail if you empty a text box or add a non number character and so on so you have to make proper checks etc.
 
Share this answer
 
v2
Comments
Wendelius 8-Jan-12 8:15am    
Did you move the definition for the parties to class level as in the example?
M.H. Shojaei 8-Jan-12 8:16am    
No,
Wendelius 8-Jan-12 8:19am    
In that case you cannot refer the parties variable from the TextChanged event since it's out of scope. Try moving the definition.
M.H. Shojaei 8-Jan-12 8:39am    
Is this right class?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartTest
{
public class Party
{
public string Name { set; get; }
public int Votes { set; get; }

private string firstTeam;
private string secondTeam;
private string thiredTeam;

public Party(string First, string Second, string Thired)
{
this.firstTeam = First;
this.secondTeam = Second;
this.thiredTeam = Thired;
}
public string FirstTeam
{
get { return firstTeam; }
set { firstTeam = value; }
}
public string SecondTeam
{
get { return secondTeam; }
set { secondTeam = value; }
}
public string ThiredTeam
{
get { return thiredTeam; }
set { thiredTeam = value; }
}
}
}
Wendelius 8-Jan-12 8:49am    
No, I meant the definition for parties variable. See the example I wrote, where the line:
List<Party> parties = new List<Party>();
is

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900