ListBox does not display text
In my TabControl
using WPF,C#.I entering Text
to ListBox
in one TabItem
from the click event in TabControl
. But the ListBox
does not display the Text
. When I debug, I can find that the ListBox
has count:1. Here is the code:
namespace Tabcontrol
{
public partial class PresetTab : UserControl //3rd Tabitem ,preset.xaml.cs
{
public PresetTab()
{
InitializeComponent();
}
public void AddPresetmenu(string pMenu)
{
menubox.Items.Add(pMenu); //menubox is listbox
}
}
}
namespace Tabcontrol
{
public partial class ToolBar : UserControl
{
PresetTab tab = new PresetTab();
public ToolBar()
{
开发者_如何学C InitializeComponent();
}
public void Click(object sender, MouseButtonEventArgs e)
{
Add("TAB MENU");
}
public void Add(string menu)
{
tab.AddPresetmenu(menu); //Im calling from tabcontrol,toolbar.xaml.cs
}
}
}
It would be easier to say for sure if you would have added your XAML code as well, but it seems to me that your adding the strings directly to the Items property and aren't applying a DataTemplate
specifying how to display the strings. So either apply a DataTemplate
which turns the string into a UIElement, e.g. a TextBlock
, or try to add the TextBlock
s in your code instead of strings.
public void AddPresetmenu(string pMenu)
{
TextBlock tb= new TextBlock();
tb.Text = pMenu;
menubox.Items.Add(tb);
}
Hope this helps, if not please include your XAML, this will make it easier to spot the problem.
精彩评论