Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定列表<;字符串>;到DataGridColumn?_C#_.net_Wpf_Silverlight_Binding - Fatal编程技术网

C# 如何绑定列表<;字符串>;到DataGridColumn?

C# 如何绑定列表<;字符串>;到DataGridColumn?,c#,.net,wpf,silverlight,binding,C#,.net,Wpf,Silverlight,Binding,我有以下XAML: <DataGrid Name="nodeDataGrid" ItemsSource="{Binding NodeList}" AutoGenerateColumns="False" RowBackground="White" AlternatingRowBackground="AliceBlue" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

我有以下XAML:

<DataGrid Name="nodeDataGrid" ItemsSource="{Binding NodeList}" AutoGenerateColumns="False" RowBackground="White"
            AlternatingRowBackground="AliceBlue" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
            Background="Silver" Margin="0,34,10,10" IsReadOnly="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" CanUserSort="True" SortDirection="Ascending" CellStyle="{StaticResource CellStyle}" />
            <DataGridTextColumn Header="Category" Binding="{Binding Path=Category}" Width="*" />
            <DataGridTemplateColumn Header="Children">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ListBox>
                            <TextBlock Text="{Binding}" />
                        </ListBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
在ServiceMapViewModel中,我有一个节点列表,如:List

节点是:

    public string Name { get; set; }

    public string Category { get; set; }

    public string Group { get; set; }

    public List<string> Children { get; set; }

    public List<string> Parents { get; set; }
公共字符串名称{get;set;}
公共字符串类别{get;set;}
公共字符串组{get;set;}
公共列表子项{get;set;}
公共列表父项{get;set;}
我的问题是如何将列表框绑定到Children属性?


<TextBlock Text="{Binding Children}" />
但是请注意,除非在节点类上实现INotifyPropertyChanged,否则将无法获得绑定中反映的属性的更新

编辑:哦,等等,你正在一个数组中绑定一个数组。然后,您将需要一个能够实际绑定集合而不是文本块的控件。或者您尝试只绑定一个节点


也许您可以更好地解释一下您要做的事情。

Children
是当前上下文的集合,因此您需要使用以下内容:

<ListBox ItemsSource="{Binding Children}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <TextBlock Text="{Binding}" />
     </DataTemplate>
   </ListBox.ItemTemplate> 
</ListBox>


您是否尝试过ItemSource={Binding Children}是的,我尝试过,但出现了一个异常,因为ItemsSource已在DataGrid中初始化。不幸的是,这不起作用。是的,我已经在ViewModel中实现了INotifyPropertyChaned,并且我对名称和类别DatagridColumn有一个正确的值!编辑:它显示为(集合)TNX其工作状态。在我的时区太晚了,所以我的大脑停止工作。我在没有任何模板的列表框中编写了一个文本块--
<ListBox ItemsSource="{Binding Children}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <TextBlock Text="{Binding}" />
     </DataTemplate>
   </ListBox.ItemTemplate> 
</ListBox>