如何绑定 List<string> 并将其集合显示到 DataGridTemplateColumn 中
How to bind List<string> and display its collection into a DataGridTemplateColumn
我正在尝试在我的 DataGrid 中的列上显示字符串项列表。但是,如果我只绑定 List 对象,它显示的是文本 (Collection)。我的 DataGrid 中的所有其他数据都很好,除了这一列。我想让它显示所述集合的内容。
在我的 ViewModel 中我有:
/// <summary>
/// The list I'm binding to the Itemsource
/// </summary>
public ObservableCollection<Foo> FooCollection
{
get
{
return _fooCollection;
}
set
{
if(_fooCollection != value)
{
_fooCollection = value;
RaisePropertyChanged(nameof(FooCollection));
}
}
}
private ObservableCollection<Foo> _fooCollection = new ObservableCollection<Foo>();
public MyViewModel()
{
FooCollection = _myServices.GetFooCollection();
}
在我的 XAML 我有:
<DataGrid
ItemsSource="{Binding FooCollection, Mode=OneWay}"
VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
BorderThickness="3"
Margin="10,10,10,17"
CanUserAddRows="False"
CanUserDeleteRows="False"
Grid.Row="2"
SelectionMode="Single"
IsReadOnly="True" >
<DataGrid.Columns>
<!--Other column definitions-->
<DataGridTextColumn
Header="Places"
Binding="{Binding Places}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
在我的数据模型中我有:
public class Foo:
{
/// <summary>
/// Roles
/// </summary>
public List<string> Places { get; set; }
//... Constructors
//... Methods
}
您可以使用值转换器,例如:
public class ListToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value!= null && value is List<string> lstStr)
{
return string.Join(",", lstStr);
}
return value?.GetType().ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException("One way converter!");
}
}
XAML:
<DataGrid ...>
<DataGrid.Resources>
<local:ListToStringConverter x:Key="lstToString"/>
</DataGrid.Resources>
<DataGridTextColumn
Header="Places"
Binding="{Binding Places, Converter="{StaticResource lstToString}"}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我正在尝试在我的 DataGrid 中的列上显示字符串项列表。但是,如果我只绑定 List 对象,它显示的是文本 (Collection)。我的 DataGrid 中的所有其他数据都很好,除了这一列。我想让它显示所述集合的内容。
在我的 ViewModel 中我有:
/// <summary>
/// The list I'm binding to the Itemsource
/// </summary>
public ObservableCollection<Foo> FooCollection
{
get
{
return _fooCollection;
}
set
{
if(_fooCollection != value)
{
_fooCollection = value;
RaisePropertyChanged(nameof(FooCollection));
}
}
}
private ObservableCollection<Foo> _fooCollection = new ObservableCollection<Foo>();
public MyViewModel()
{
FooCollection = _myServices.GetFooCollection();
}
在我的 XAML 我有:
<DataGrid
ItemsSource="{Binding FooCollection, Mode=OneWay}"
VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
BorderThickness="3"
Margin="10,10,10,17"
CanUserAddRows="False"
CanUserDeleteRows="False"
Grid.Row="2"
SelectionMode="Single"
IsReadOnly="True" >
<DataGrid.Columns>
<!--Other column definitions-->
<DataGridTextColumn
Header="Places"
Binding="{Binding Places}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
在我的数据模型中我有:
public class Foo:
{
/// <summary>
/// Roles
/// </summary>
public List<string> Places { get; set; }
//... Constructors
//... Methods
}
您可以使用值转换器,例如:
public class ListToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value!= null && value is List<string> lstStr)
{
return string.Join(",", lstStr);
}
return value?.GetType().ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException("One way converter!");
}
}
XAML:
<DataGrid ...>
<DataGrid.Resources>
<local:ListToStringConverter x:Key="lstToString"/>
</DataGrid.Resources>
<DataGridTextColumn
Header="Places"
Binding="{Binding Places, Converter="{StaticResource lstToString}"}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>