tags:

views:

58

answers:

1

In my tabcontrol using WPF,C#.I entering text to listbox in one tabitem from the click event in tabcontrol.But the listbox doesnot display the text.When i debug i can find tht 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()
        {            
            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      
        }
    }
}
A: 

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 TextBlocks 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.

elmar
My XAML:<UserControl x:Class="Tabcontrol.PresetTab" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Height="800" Width="260" FontFamily="Arial"><Grid Width="250" Height="800"><StackPanel Orientation="Vertical"><Border Margin="-5,5,0,0" Background ="Green"CornerRadius="10,10,0,0" Width="240" Height="40" VerticalAlignment="Top"></Border><ListBox Name="menubox" Margin="0,5,0,0" Height="244" Width="240" Background="Silver" BorderThickness="0"></ListBox> </StackPanel></Grid></UserControl>
Anu
As I expected, you're adding the string directly to the Items collection. Have you tried my code? Or else the DataTemplate solution I suggested?
elmar
Sorry,I cudnt understand ur code.U assign pMenu to textblock,and again u assign the same pMenu to listbox.Wats the difference between my code and ur code.And also i dont know DataTEmplate.Can u pls send me any sample on that.
Anu
Sorry that was a typo, I should have added the textbox. I corrected my code.
elmar
You might also find the following link on DataTemplating useful: http://msdn.microsoft.com/en-us/library/ms742521.aspx#DataTemplating_Basic. I would also recommend you to read the guided tour of WPF by Josh Smith: http://www.codeproject.com/KB/WPF/GuidedTourWPF_1.aspx
elmar