DataGrid刷新问题 [英] DataGrid refresh issue

查看:98
本文介绍了DataGrid刷新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 4 DataGrid ,其 ItemsSource 绑定到 ObservableCollection 。当我修改我的 ObservableCollection 的元素时,修改的元素正确显示在我的网格中,除了一个的元素。此列与其他的 TemplateColumn 的方式不同,它使用的是 ValueConverter



>列的模板包含一个简单的stackPanel,它包含一个Path控件和一个Label。而Label在一个简单的ValueConverter的帮助下绑定到一些Source对象。



现在的问题是当我修改属于ObservableCollection的一些元素除了上述之外,网格的所有列均正确显示。它只是保持不变 - 但是当我使用mousecursor选择DataGridCell并再次单击它,所需的刷新突然发生。



所以我想这是一个简单的东西我在这里失踪,但我找不到...



提前感谢。



编辑:



同时我能够进一步找到问题:似乎在修改了我的ObservableCollection属于我的网格中绑定到源的标签的相应ValueConverter根本不被调用。当我单击单元格内ValueConverter正在调用,因为它应该。但是它不会自动 - 那么我该如何实现呢?请帮助:)



编辑:



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

这是我的ValueConverter的代码:
(但我不认为转换器的代码与我的问题有任何关系,我只在这里发布完整性。)

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

string trip =;

if(value!= null){


列表< Leg> leg =(List< Leg>)值;

if(legs.Count> = 1){

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

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

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

回程;
}


解决方案

code>路径通知需要到位,所以这两个类都拥有 Route 和拥有的类需要实现 c code code

Legs 列表自然没有更新,实际上即使 Legs 属性的类型为 ObservableCollection< ...> ; ,这并不重要,因为绑定引擎只关心 INPC



所以如果您希望绑定更新,如果收集更改,您需要为 Legs 属性更改属性,每次修改它(包括完全替换参考)时,都会更改。


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;
    }

解决方案

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

这篇关于DataGrid刷新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆