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:
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
<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
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.