Silverlight 将datagrid数据绑定到LocationCollection

Silverlight 将datagrid数据绑定到LocationCollection,silverlight,data-binding,Silverlight,Data Binding,我对datagrid和MapPolyLine LocationCollection有一个小问题。目前,我已经创建了一个网格,其中有两列,一列表示经度,一列表示纬度。我已将datagrid的ItemsSource设置为我的LocationCollection,并已将列绑定到相应的位置值,如下所示 var lonCol=new DataGridTextColumn{Header=Longitude,Binding=new BindingLongitude{Mode=BindingMode.TwoWa

我对datagrid和MapPolyLine LocationCollection有一个小问题。目前,我已经创建了一个网格,其中有两列,一列表示经度,一列表示纬度。我已将datagrid的ItemsSource设置为我的LocationCollection,并已将列绑定到相应的位置值,如下所示

var lonCol=new DataGridTextColumn{Header=Longitude,Binding=new BindingLongitude{Mode=BindingMode.TwoWay}

这一切都很好,但是,我实际上想让网格有一个单独的列,整个坐标作为它的显示值,原因是我可能想在不同的坐标系UTM中显示该项。例如,我想我可以通过某种方式使用值转换器来实现这一点。我的问题是,我看不到我如何使我的网格有一个列绑定到我的LocationCollection中的项目并显示其ToString值,其次,我如何能够根据某种标志转换显示的值

对不起,如果这没有解释好


非常感谢所有帮助

通过使用值转换器,您可以在数据网格中只使用一列

// If you don't specify a property path, Silverlight uses the entire object
// For two way binding make sure you bind it to an IList<LocationWrapper>
var theOneColumn = new DataGridTextColumn ( 
    Header = "Long + Lat",
    Binding = new Binding(){ 
        Mode = BindingMode.TwoWay,  
        Converter = new LongLatConverter()
    }
);


//Converter
public class LongLatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var loc = value as LocationWrapper;
        return String.Format("Long: {0}, Lat: {0}", loc.Longitude, loc.Latitude);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //...
    }
}

//Wrapper class for Location so two way binding works
public class LocationWrapper
{
    //The actual location object
    public String Location( get; set;}      
    public LocationWrapper(Location loc)
    {
        this.Location = loc;
    }
}

非常感谢您提供的信息,但是当我以这种方式创建带有一列的datagrid时,当一个项目添加到itemssource集合(即位置集合)时,我收到一个异常,通知我双向绑定需要路径,有什么想法吗?:-似乎我忽略了这个双向绑定问题。。。您可以始终为Location创建一个自定义包装器类,该类将Location作为其属性。我将更新我的答案以给出一个例子。
var theOneColumn = new DataGridTextColumn ( 
    Header = "Long + Lat",
    Binding = new Binding(){ 
        Mode = BindingMode.TwoWay,  
        Converter = new LongLatConverter(),
        ConverterParameter = "UTM" // This is what you want to set
    }
);

// ....
// In the Value Converter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var loc = value as Location;

    if( (parameter as string).equals("UTM"))
    {
        //return UTM formated long/lat coordinates 
    }
    else
    {
        return String.Format("Long: {0}, Lat: {0}", loc.Longitude, loc.Latitude);
    }
}