Click here to Skip to main content
14,836,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
wrote valueconverter.
it works,
but when I update my viewLists, the converter doesn't work.

what is the problem?
And can I solve it?

thanks.

What I have tried:

I have a listview:

<ListView x:Name="studentWithValueConvector" Margin="5" Background="Beige">
<pre><ListView.ContextMenu>
					<ContextMenu>
						<MenuItem Header="Delete"  Click="Del"/>
						<MenuItem Header="Update" Click="Update" />				</ContextMenu>
<ListView.View>
		<GridView>
			<GridViewColumn Header="Average"  DisplayMemberBinding="{Binding UpdateSourceTrigger=PropertyChanged, Converter={StaticResource gradesRemarkConvertor}}" Width="140"/>
		</GridView>
	</ListView.View>
</ListView>


I wrote valueconverter:


public class GradeRemarkConvector : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           Student stu = value as Student;
           if (stu.GradesAvg < 56)
               return "VERY BAD";
           else if (stu.GradesAvg < 80)
               return "MEDIUM";
           return "VERY GOOD";
       }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           throw new NotImplementedException();
       }
   }


The get/set of the Student class:
public class Student : INotifyPropertyChanged
{
    
    private double gradesAvg; 

    public Student() { }
    public double GradesAvg
    {
        get { return gradesAvg; }
        set
        {
            if (gradesAvg != value)
            {
                gradesAvg = value;

                OnPropertyChanged("GradesAvg");
            }

        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


}
<pre>

The value coverter works, but when I update the viewlist, I dont see the changes.

here is the upadate :

private void btnUpdate_Click(object sender, RoutedEventArgs e)
       {

           stu.GradesAvg = int.Parse(txtAvg.Text.ToString());
           btnAddStudent.Visibility = Visibility.Visible;
           btnUpdateNames.Visibility = Visibility.Collapsed;

           txtAvg.Text = string.Empty;


       }
Posted
Updated 7-Dec-20 23:00pm

1 solution

I successed solving it!!!
I needed to refresh the viewlist so after i updated the listview item,I added the next two lines:
studentWithValueConvector.ItemsSource = null;
           studentWithValueConvector.ItemsSource = students;


that made the listview refreshing, and it worked!!
   

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