Bip
Bip

Reputation: 913

How to bind a list<string> to DataGridColumn?

I have a following 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>

And a code behind:

    private ServiceMapViewModel smViewModel = new ServiceMapViewModel();

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

            };
    }

In ServiceMapViewModel I have a NodeList like: List < Node >

And Node is:

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

My question is how to bind a listbox to Chidren property?

Upvotes: 1

Views: 3670

Answers (2)

Matt Burland
Matt Burland

Reputation: 45155

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

Note however that you will not get updates to the property reflected in the binding unless you implement INotifyPropertyChanged on your Node class.

Edit: Oh wait, you are binding an array within an array. Then you'll need a control that can actually bind a collection rather than a TextBlock. Or where you trying to just bind one node?

Maybe you can explain a little better what you are trying to do.

Upvotes: 0

devdigital
devdigital

Reputation: 34369

Children is a collection on the current context, so you will need to use the following:

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

Upvotes: 3

Related Questions