0
  • I'm very new to WPF (especially XAML) *

Hello, my app has a class which gets bunch of comma separated string. So I made a List collection to store the strings. Also, I made DataTemplate for the listbox. Here is code.

MainWindow.xaml

...
<DataTemplate x:Key="DataTemplate">
    <Grid>
        <StackPanel Grid.Column="1" Margin="5">
            <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
                <TextBlock Text="{Binding AAA}" />
            </StackPanel>
            <TextBlock Text="{Binding BBB}" />
            <TextBlock Text="{Binding CCC}" />
        </StackPanel>
    </Grid>
</DataTemplate>
...
<ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="309" Margin="10,10,0,0" Width="216" BorderThickness="1" VerticalAlignment="Top" ItemTemplate="{DynamicResource HeadlineDataTemplate}"/>

MainWindow.xaml.cs

...
MyClass myClass;

public MainWindow()
{
    InitializeComponent();
    myClass = new MyClass();
}
...
private void Button_Click(object sender, RoutedEventArgs e)
{
    myClass.getData(Textbox1.Text); // I want to convert this to add items to listbox1.
}

MyClass.cs

...
public void getData(string target)
{
    List<string> itemList = new List<string>();
    ...
    while(result != null)
    {
        // This loop gives bunch of comma separated string (more than 100)
        itemList.Add(result);
    }
    // after the loop, itemList has
    // itemList[0] = {"AAA1,BBB1,CCC1"}
    // itemList[1] = {"AAA2,BBB2,CCC2"}
    // itemList[2] = {"AAA3,BBB3,CCC3"}
    // ...
    // I also can store the strings to List<string[]> using string.split().

}

So how should I do this?

I couldn't find the answer on the internet.

1
  • i would start with undersanding data binding, google several simple exampls such as this (follow the links) stackoverflow.com/questions/10912846/… Commented Jan 12, 2014 at 7:28

2 Answers 2

2

I suggest to create a model class to represent each ListBox Item. In this example I name it MyListBoxItemModel :

public class MyListBoxItemModel
{
    public string AAA { get; set; }
    public string BBB { get; set; }
    public string CCC { get; set; }
}

Then in the getData function, create List of MyListBoxItemModel to be listbox1's ItemsSource :

public void getData(string target)
{
    List<MyListBoxItemModel> itemList = new List<MyListBoxItemModel>();
    ...
    while(result != null)
    {
        var splittedResult = result.Split(',');
        itemList.Add(new MyListBoxItemModel{ 
                                AAA = splittedResult[0], 
                                BBB = splittedResult[1], 
                                CCC = splittedResult[2] 
                            });
    }
    listbox1.ItemsSource = itemList;
}

Note that this example only demonstrates minimum amount of code needed to get your data displayed in the ListBox. Not involving various techniques and best practices around WPF development, such as implementing INotifyPropertyChanged interface, MVVM pattern, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought about this, but would it make my app slower? cause it creates instance every each time in the while loop.
I don't think so. Because it is just a plain class with no logic in it's constructor. So far I see that binding to model is more common practice then binding to array of string
1

you can just bind to public properties, so if you write something like

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

you need an object with a public property "BBB".

so in addition to the answer from har07 you should also use a public property for your list(OberservableCollection)

 public OberservableCollection<MyListBoxItemModel> ItemList {get;set;}

then your binding for your itemscontrol can look like this

<ListBox ItemsSource="{Binding ItemList}" ItemTemplate="{DynamicResource HeadlineDataTemplate}"/>

all you need now is the right datacontext ;)

2 Comments

Thanks! What's the benefit of using ObservableCollection?
observablecollection implement notification for Add() Remove(), Clear() - so you see all changes in your collection in your view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.