2016-04-12 6 views
0

Ich habe eine wpf ListBox, ich konvertierte das DataSet in string [] -Array, und ich versuche, das Array in der Listbox anzuzeigen, aber es wird nicht angezeigt.Display string [] Array in einer ListBox

Das ist mein Code !!!

string query = " SELECT category_id, category_name, amount FROM acc_income_category WHERE deleted = 0 ORDER BY category_name ASC "; 
da = new SQLiteDataAdapter(query, con); 
ds = new DataSet(); 
da.Fill(ds, "acc_income_category"); 
string[] arr = new string[ds.Tables[0].Rows.Count]; 
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
{ 
    arr[i] = ds.Tables[0].Rows[i]["category_name"].ToString(); 
} 
ListBox_Category_Names.ItemsSource = arr; 

und das ist mein WPF ListBox XAML-Code

<ListBox x:Name="ListBox_Category_Names" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
    Width="auto" Height="auto" SelectionMode="Multiple" ItemsSource="{Binding acc_income_category}" Grid.Row="1" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid HorizontalAlignment="Stretch" > 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="auto" /> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="auto" /> 
       </Grid.ColumnDefinitions> 
       <CheckBox x:Name="chkBox_CheckCategory" Grid.Column="0" Width="35" Height="35" Cursor="Hand" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}" /> 

       <!--<TextBlock Text="{Binding category_name}" Grid.Column="1" FontSize="15" Foreground="#FF666666" Margin="10,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Left" /> 
       <TextBlock Text="{Binding amount, ConverterCulture=ig-NG, StringFormat=\{0:C\}}" Grid.Column="3" FontSize="15" Foreground="#FF666666" Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>--> 

      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+1

Warum String-Array? Binding verwenden, siehe: http://stackoverflow.com/questions/12363369/how-can-i-bind-a-listbox-to-a-data-table-in-wpf-applications –

+0

Was passiert, wenn Sie einfach die '.ToList()' um zurück an die ListBox zu binden 'ListBox_Category_Names.ItemsSource = arr.ToList();' versuchen Sie es im folgenden für einige Stackoverflow Beispiele 'C# stack overflow Binding Listbox zu Liste ' gibt es viele Möglichkeiten zu tun dies – MethodMan

+0

https://msdn.microsoft.com/en-us/library/aa480224.aspx –

Antwort

0

Stück Code Nach arbeitet mit Anugular-JS

var USState = ["Alabama", "Alaska" , "Arizona" ,"Arkansas", "California", 
     "Colorado", "Connecticut"]; 

$(document).ready(function() 
{ 
stateselect = document.getElementById("state"); 
for(i=0; i < USState.length; ++i) 
{ 
    var el=document.createElement('option'); 
    el.textContent = USState[i]; 
    stateselect.appendChild(el); 
} 

});