问题
Here is code of class:
public class listboxitem
{
public string textmenu { get; set; }
public string logomenu { get; set; }
}
But when im binding it on textbox that's not show...
I have these array:
private string[] Logo_menu_array = { "/Assets/star-6-48.ico", "/Assets/note-48.ico", "/Assets/note-48.ico", "medal-48.ico", "joystick-48.ico" };
private string[] Text_menu_array={"Phổ biến trên YouTuBe","Âm nhạc","Thể thao","Trò chơi"};
//load menu
public void Load_Menu()
{
List<listboxitem> text = new List<listboxitem>();
listboxitem items=new listboxitem();
for(int i=0;i<Text_menu_array.Length&& i<Logo_menu_array.Length;i++)
{
items.textmenu=i.ToString();
}
for(int j=0;j<Logo_menu_array.Length;j++)
{
items.logomenu = j.ToString();
}
text.Add(items);
}
This website didn't agreed show more code. Ready hard to ask these my question. I added code:
<ListBox Name="lst_menu" Foreground="Red">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding logomenu}"></Image>
<TextBlock Text="{Binding textmenu}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And here to loaded:
public MainPage()
{
this.InitializeComponent();
//get menu
List<listboxitem> menu_list = new List<listboxitem>();
Load_Menu();
lst_menu.ItemsSource = menu_list;
}
回答1:
A few things here...
Firstly, we need to see the XAML to see how you are binding this in your UI. I'm going to assume you have something like:
<ListBox ItemSource="{Binding Items}"/>
Then a template that presents these.
In your code you have a List object called text which exists only in your method. You need to assign this value to a property in your view model onto which you can bind - based on the above:
public List<ListItem> Items {get;set;}
This property should trigger PropertyChanged as defined in INotifyPropertyChanged so implement that on your class. Implementing INotifyPropertyChanged
That gets you the basics. If you want to dynamically control this collection - i.e. change items at runtime - you should investigate ObservableCollection.
EDIT: Based upon your full code listing, using code behind. You are setting your lst_menu.ItemsSource to an empty List. Your Load_Menu() builds a collection but does not return it.
public List<listboxitem> Load_Menu()
{
List<listboxitem> text = new List<listboxitem>();
listboxitem items=new listboxitem();
for(int i=0;i<Text_menu_array.Length&& i<Logo_menu_array.Length;i++)
{
items.textmenu=i.ToString();
}
for(int j=0;j<Logo_menu_array.Length;j++)
{
items.logomenu = j.ToString();
}
text.Add(items);
return text;
// Note you will only ever return one item here - check the logic
}
Then in your constructor:
List<listboxitem> menu_list = Load_Menu();
lst_menu.ItemsSource = menu_list;
Assuming you want the arrays to be used to build your collection, try using the following for build menu:
List<listboxitem> text = new List<listboxitem>();
for(int i =0; i< Math.Min(Logo_menu_array.Length, Text_menu_array.Length, i++)
{
var l = new listboxitem();
l.logomenu = Logo_menu_array[i];
l.textmenu = Logo_menu_array[i];
}
return text;
I hope this helps.
来源:https://stackoverflow.com/questions/25133244/unable-binding-from-class-on-c-sharp