Click here to Skip to main content
15,312,257 members
Home / Discussions / WPF
   

WPF

 
QuestionHow to bind a dataset to a WPF richtextbox? Pin
fsunole0329-Jan-14 10:43
Memberfsunole0329-Jan-14 10:43 
AnswerRe: How to bind a dataset to a WPF richtextbox? Pin
Wayne Gaylard30-Jan-14 1:07
professionalWayne Gaylard30-Jan-14 1:07 
QuestionHandling RowEditEnding in a DataGrid according to MVVM pattern Pin
Piotr Z27-Jan-14 23:43
MemberPiotr Z27-Jan-14 23:43 
AnswerRe: Handling RowEditEnding in a DataGrid according to MVVM pattern Pin
Pete O'Hanlon28-Jan-14 0:29
mvaPete O'Hanlon28-Jan-14 0:29 
GeneralRe: Handling RowEditEnding in a DataGrid according to MVVM pattern Pin
Piotr Z28-Jan-14 0:51
MemberPiotr Z28-Jan-14 0:51 
GeneralRe: Handling RowEditEnding in a DataGrid according to MVVM pattern Pin
Pete O'Hanlon28-Jan-14 1:10
mvaPete O'Hanlon28-Jan-14 1:10 
AnswerRe: Handling RowEditEnding in a DataGrid according to MVVM pattern Pin
_Maxxx_2-Feb-14 19:36
professional_Maxxx_2-Feb-14 19:36 
QuestionProblems updating datagrid from collection Pin
abollmeyer23-Jan-14 10:55
Memberabollmeyer23-Jan-14 10:55 
Can someone please explain to me what I'm doing wrong. My goal is to update a datagrid after entering values. For instance, I put 3 grades into 3 columns, then click a button and update the datagrid to achieve an average.

My C# code:

C#
<pre>public partial class MainWindow : Window
    {
        public Student student = new Student();
        public ObservableCollection<Student> studentData = new ObservableCollection<Student>();
        public MainWindow()
        {
            InitializeComponent();

            //Set initial values
            student.Name = "Whatever";
            student.CalculusMarks = 0;
            student.EnglishMarks = 0;
            student.ProgrammingMarks = 0;
            student.Percentage = 0;

            //Read values to collection
            studentData.Add(new Student { Name=student.Name, CalculusMarks = student.CalculusMarks, EnglishMarks = student.EnglishMarks, ProgrammingMarks = student.ProgrammingMarks, Percentage=student.Percentage});

            //Set data context
            datagrid1.DataContext = studentData;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Perform calculation
            student.Percentage = (double)(student.CalculusMarks + student.EnglishMarks + student.ProgrammingMarks) * 100 / 300;
        }

        public class Student : INotifyPropertyChanged
        {
            private string _name;
            private int _calculusMarks;
            private int _englishMarks;
            private int _programmingMarks;
            private double _percentage;

            public string Name
            {
                get
                {
                    return _name;
                }

                set
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }

            public int CalculusMarks
            {
                get
                {
                    return _calculusMarks;
                }

                set
                {
                    _calculusMarks = value;
                    OnPropertyChanged("CalculusMarks");
                }
            }

            public int EnglishMarks
            {
                get
                {
                    return _englishMarks;
                }

                set
                {
                    _englishMarks = value;
                    OnPropertyChanged("EnglishMarks");
                }
            }

            public int ProgrammingMarks
            {
                get
                {
                    return _programmingMarks;
                }

                set
                {
                    _programmingMarks = value;
                    OnPropertyChanged("ProgrammingMarks");
                }
            }

            public double Percentage
            {
                get
                {
                    return _percentage;
                }

                set
                {
                    _percentage = value;
                    OnPropertyChanged("Percentage");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;    

            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }


My XML:

HTML
<<pre>Grid>
        <DataGrid Name="datagrid1" AutoGenerateColumns="False" ItemsSource="{Binding}">
            <DataGrid.Columns>    
                <DataGridTextColumn x:Name="NameCol" Header="Name" Width="*" Binding="{Binding Path=Name, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn x:Name="CalculusMarksCol" Header="Calculus" Width="*" Binding="{Binding Path=CalculusMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn x:Name="EnglishMarksCol" Header="English" Width="*" Binding="{Binding Path=EnglishMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn x:Name="ProgrammingMarksCol" Header="Programming" Width="*" Binding="{Binding Path=ProgrammingMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn x:Name="PercentageCol" Header="Percentage" Width="*" Binding="{Binding Path=Percentage, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
            </DataGrid.Columns>
        </DataGrid>
        <DockPanel LastChildFill="False">
            <Button Content="Update" Height="50" Width="75" Margin="5" DockPanel.Dock="Bottom" Click="Button_Click" />
        </DockPanel>
    </Grid
>

I have searched exhaustively and am still no closer to an answer. Thanks in advance.
AnswerRe: Problems updating datagrid from collection Pin
Mycroft Holmes23-Jan-14 12:04
professionalMycroft Holmes23-Jan-14 12:04 
QuestionBinding property from XML file data Pin
abollmeyer21-Jan-14 9:18
Memberabollmeyer21-Jan-14 9:18 
AnswerRe: Binding property from XML file data Pin
abollmeyer21-Jan-14 14:44
Memberabollmeyer21-Jan-14 14:44 
QuestionCombobox / Textblock field Pin
eddieangel21-Jan-14 6:24
Membereddieangel21-Jan-14 6:24 
AnswerRe: Combobox / Textblock field Pin
Jason Gleim21-Jan-14 7:52
professionalJason Gleim21-Jan-14 7:52 
GeneralRe: Combobox / Textblock field Pin
eddieangel21-Jan-14 8:07
Membereddieangel21-Jan-14 8:07 
GeneralRe: Combobox / Textblock field Pin
Jason Gleim21-Jan-14 8:48
professionalJason Gleim21-Jan-14 8:48 
GeneralRe: Combobox / Textblock field Pin
eddieangel21-Jan-14 9:14
Membereddieangel21-Jan-14 9:14 
GeneralRe: Combobox / Textblock field Pin
Jason Gleim21-Jan-14 9:42
professionalJason Gleim21-Jan-14 9:42 
GeneralRe: Combobox / Textblock field Pin
eddieangel21-Jan-14 10:12
Membereddieangel21-Jan-14 10:12 
GeneralRe: Combobox / Textblock field Pin
Jason Gleim21-Jan-14 10:25
professionalJason Gleim21-Jan-14 10:25 
GeneralRe: Combobox / Textblock field Pin
eddieangel21-Jan-14 11:29
Membereddieangel21-Jan-14 11:29 
GeneralRe: Combobox / Textblock field Pin
Jason Gleim22-Jan-14 4:28
professionalJason Gleim22-Jan-14 4:28 
GeneralRe: Combobox / Textblock field Pin
eddieangel22-Jan-14 5:34
Membereddieangel22-Jan-14 5:34 
QuestionHow to stop repeating animation Pin
Member 1001614016-Jan-14 12:25
MemberMember 1001614016-Jan-14 12:25 
AnswerRe: How to stop repeating animation Pin
Varsha Ramnani20-Jan-14 22:41
professionalVarsha Ramnani20-Jan-14 22:41 
QuestionWPF Checkbox Binding Question Pin
Kevin Marois10-Jan-14 8:38
professionalKevin Marois10-Jan-14 8:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.