如何将列表<string>绑定到DataGridColumn?</string>

时间:2012-03-08 23:27:26

标签: 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" 
            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>

背后的代码:

    private ServiceMapViewModel smViewModel = new ServiceMapViewModel();

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>
            {
                this.DataContext = smViewModel;

            };
    }

在ServiceMapViewModel中我有一个NodeList,如:List&lt;节点&gt;

节点是:

    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; }

我的问题是如何将列表框绑定到Chidren属性?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

<TextBlock Text="{Binding Children}" />

但请注意,除非在Node类上实现INotifyPropertyChanged,否则不会对绑定中反映的属性进行更新。

编辑:哦等等,你绑定一个数组中的数组。然后你需要一个实际上可以绑定集合而不是TextBlock的控件。或者您尝试绑定一个节点的位置?

也许你可以更好地解释一下你想做什么。

相关问题