DataGrid refresh issue

蓝咒 提交于 2019-12-12 03:09:57

问题


I have a Silverlight 4 DataGrid which has its ItemsSource bound to an ObservableCollection. When i modify an element of my ObservableCollection the modified element is correctly displayed inside my grid except the element of one column. This columns differs from the others in the way it is a TemplateColumn and it's using a ValueConverter.

The Template for the column consists of a simple stackPanel that includes a Path control and a Label. And the Label is bound to some Source object with the help of a simple ValueConverter.

The problem now is when i modify some element that belongs to the ObservableCollection all columns of the grid are displayed correctly except the one described above. It simply stays unchanged - but when i use the mousecursor to select the DataGridCell and click it a second time, the desired refresh suddenly happens.

So I guess it's something simple what i am missing here, but I can't find it ...

Thanks in advance ..

EDIT:

In the meanwhile I was able to further locate the problem: It seems that after I modify an element of my ObservableCollection the corresponding ValueConverter that belongs to the label that is in my grid that is bound to the source is simply not called. When i click inside the cell the ValueConverter is getting called as it should. BUT it won't automatically - So how do I achieve that ? please help :)

EDIT:

The binding:

<sdk:Label Content="{Binding Route.Legs, Converter={StaticResource IncomingTableRouteTripConverter}}" Margin="9,0,0,0" Style="{StaticResource TripLabelTemplate}" FontFamily="Arial" FontSize="10.667" Padding="0" Height="10" FontWeight="Bold" />

This is the code of my ValueConverter: (But I don't think that the code of the converter has anything to do with my problem I only posted it here for completeness)

public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

        string trip = "";

        if (value != null) {


            List<Leg> legs = (List<Leg>)value;

            if (legs.Count >= 1) {

                for (int i = 0; i <= legs.Count - 1; i++) {

                    trip += ((Leg)legs[i]).Start.ICAO + " - " + ((Leg)legs[i]).Stop.ICAO + " - ";
                }

                trip = trip.Substring(0, trip.Length - 2);
            }
        }

        return trip;
    }

回答1:


For all nodes in the Path notifications need to be in place, so both the class owning Route and the class owning Legs need to implement INPC.

Further if you add items to the Legs list naturally nothing will be updated, in fact even if the Legs property were of type ObservableCollection<...> that would not matter as the binding engine only cares about INPC.

So if you want the binding to update if the collection changes you need to fire property changed for the Legs property every time it somehow is modified (including a complete replacement of the reference).




回答2:


IF you use like

Content="{Binding Path=Parameter Converter={StaticResource SomeConverter}}"

then your problem might be solved...



来源:https://stackoverflow.com/questions/9172931/datagrid-refresh-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!