【问题标题】:How to bind List<string> and display its collection into a DataGridTemplateColumn如何绑定 List<string> 并将其集合显示到 DataGridTemplateColumn
【发布时间】:2021-02-08 15:49:55
【问题描述】:

我正在尝试在我的 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
}

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    您可以使用值转换器,例如:

    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>
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 2014-05-02
      • 2021-06-28
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多