relliv
relliv

Reputation: 853

Wpf listbox binding with string array

i have a listbox and this my xaml code;

        <ListBox x:Name="DepremlerListesi" Margin="0,0,542,14">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="332" Background="#4CFFFFFF">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="132*"/>
                        <ColumnDefinition Width="200*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="40"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid Grid.RowSpan="3" Margin="0,0,12,0"
                          Background="Orange"
                          Width="120"
                          Height="120"
                          HorizontalAlignment="Left">
                        <TextBlock Text="{Binding XX}"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   FontSize="48" Foreground="White"/>
                    </Grid>
                    <TextBlock Grid.Column="1" Grid.ColumnSpan="3"
                               Text="{Binding YY}"
                               FontSize="16"
                               VerticalAlignment="Center"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Text="Başlık" TextWrapping="Wrap"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="6"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

There is a string array in my hand. I want to add this array and that ListBox. I've searched a lot but I couldn't find enough information.

I tried (this page) the answers on the following page,

        ObservableCollection<string> oList;
        oList = new ObservableCollection<string>(MyArray);
        DepremlerListesi.DataContext = oList;

        Binding binding = new Binding();
        DepremlerListesi.SetBinding(ItemsControl.ItemsSourceProperty,binding);

        (DepremlerListesi.ItemsSource as ObservableCollection<string>).RemoveAt(0);

but the result (the yellow boxes are empty);

enter image description here

the values it's supposed to be this way;

enter image description here

With the list maybe it could be, but I don't know how. (this article)

If you're wondering if what I want to do, i have a work on earthquakes. This data is the severity of the earthquake. I'm curious about solution suggestions. I hope my broken English with i told description. Thanks.

Upvotes: 0

Views: 6869

Answers (2)

Josep B.
Josep B.

Reputation: 587

The best way is to set the binding directly in the xaml:

Add this in the xaml code:

<ListBox ItemsSource="{Binding DepremlerListesi}" Margin="0,0,542,14">

After that:

public MainWindow()
{
    this.DataContext = this;

    InitializeComponent();

    DepremlerListesi = new ObservableCollection<ClassToBind>();
    DepremlerListesi.Add(new ClassToBind("test1", "test2"));
    DepremlerListesi.Add(new ClassToBind("test3", "test4"));
    DepremlerListesi.Add(new ClassToBind("test5", "test6"));
    OnPropertyChanged("DepremlerListesi");
}

And I created the auxiliar class:

public class ClassToBind
{     
    public string XX { get; set; }
    public string YY { get; set; }      

    public ClassToBind(string var1, string var2)
    {
        XX = var1;
        YY = var2;
    }
}

Upvotes: 1

SledgeHammer
SledgeHammer

Reputation: 7690

Whats XX and YY? If you just want to bind a string array to the listbox, then set the listbox.ItemsSource to the array and simply use "{Binding}", exactly like that and it will bind to the item itself which in this case is the string.

Upvotes: 1

Related Questions