DataGrid刷新问题
我有一个 Silverlight 4 DataGrid,其 ItemsSource 绑定到 ObservableCollection。当我修改 ObservableCollection 的元素时,除了一个列 的元素之外,修改后的元素会正确显示在网格内。此列与其他列的不同之处在于它是 TemplateColumn 且使用 ValueConverter。
该列的模板由一个简单的 stackPanel 组成,其中包括一个 Path 控件和一个 Label。并且标签在一个简单的 ValueConverter 的帮助下绑定到某个 Source 对象。
现在的问题是,当我修改属于 ObservableCollection 的某个元素时,网格的所有列都会正确显示,除了上面描述的列之外。它只是保持不变 - 但是当我使用鼠标光标选择 DataGridCell 并再次单击它时,所需的刷新突然发生。
所以我想我在这里缺少的东西很简单,但我找不到它......
提前致谢......
编辑:
同时我能够进一步找到问题:似乎在我修改 ObservableCollection 的元素后,属于绑定到源的网格中的标签的相应 ValueConverter 根本没有被调用。当我在单元格内单击时,ValueConverter 将按其应有的方式被调用。但它不会自动 - 那么我该如何实现呢?请帮助:)
编辑:
绑定:
<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" />
这是我的 ValueConverter 的代码: (但我不认为转换器的代码与我的问题有任何关系,我只是为了完整性才将其发布在这里)
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
Path
中的所有节点,通知都需要就位,因此拥有Route
的类和拥有Legs
的类都需要实现INPC
。此外,如果您将项目添加到
Legs
列表中,自然不会更新任何内容,事实上,即使Legs
属性的类型为ObservableCollection<...>< /code> 这并不重要,因为绑定引擎只关心
INPC
。因此,如果您希望在集合更改时更新绑定,则需要在每次以某种方式修改(包括完全替换引用)时触发 Legs 属性的属性更改。
For all nodes in the
Path
notifications need to be in place, so both the class owningRoute
and the class owningLegs
need to implementINPC
.Further if you add items to the
Legs
list naturally nothing will be updated, in fact even if theLegs
property were of typeObservableCollection<...>
that would not matter as the binding engine only cares aboutINPC
.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).如果您使用
Content="{Binding Path=Parameter Converter={StaticResource SomeConverter}}"
那么您的问题可能会得到解决...
IF you use like
Content="{Binding Path=Parameter Converter={StaticResource SomeConverter}}"
then your problem might be solved...