2013-10-05 31 views
2

我試圖在代碼behind.And最後附上該StackPanel中,以網格control.My預期的特定列一個StackPanel添加標籤和圖像的設置輸出應該像動態添加內容在代碼中的StackPanel背後

<image><label> 

組合

,但我的輸出就像它顯示在網格的第一列,並在下一列圖像的標籤。(我是不能添加的快照,因爲我沒有足夠的聲譽)

XAML代碼

<Window x:Class="Ping.MainWindow" WindowStyle="ThreeDBorderWindow" Icon="F:\ChatApplication\Ping\Ping\Images\title.ico" Cursor="Pen" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Ping - Connected by alphabets" Height="450" Width="750" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" > 
<Grid Name="grid_window"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="200"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <GridSplitter HorizontalAlignment="Right" 
       VerticalAlignment="Stretch" 
       Grid.Column="1" ResizeBehavior="PreviousAndNext" 
       Width="5" Background="#FFBCBCBC"/> 

    <TabControl Grid.ColumnSpan="2" Name="tab_control" BorderBrush="Cornsilk" BorderThickness="4" HorizontalAlignment="Left" Height="419" Margin="227,0,0,0" VerticalAlignment="Top" Width="515"> 
     <TabItem Header="Home" BorderBrush="Green"/> 
    </TabControl> 
</Grid> 

代碼背後

public MainWindow() 
    { 
     InitializeComponent(); 
     DBCoding dbobj = new DBCoding(); 
     //Contains names {"Dhivi","Walle"} 
     List<string> online = new List<string>(); 

     online = dbobj.onlineUsers(); 
     StackPanel myStackPanel = new StackPanel(); 
     myStackPanel.Orientation = Orientation.Vertical; 
     foreach (var item in online) 
     { 
      Image myImage = new Image(); 
      myImage.Source = new BitmapImage(new Uri("F:\\ChatApplication\\Ping\\Ping\\Images\\visible.png")); 
      myImage.Width = 10; 
      myImage.Height = 10; 
      //myImage.Margin = new Thickness(-10,0,-80,0); 
      myImage.Height = 10; 
      Label user = new Label(); 
      user.Content = item.ToString(); 
      myStackPanel.Children.Add(myImage); 
      myStackPanel.Children.Add(user); 
     } 
     grid_window.Children.Add(myStackPanel); 
     Grid.SetColumnSpan(myStackPanel, 1); 
     Grid.SetColumn(myStackPanel, 0); 

    } 

誰能告訴我的解決方案。

+0

它不是來自這個問題清楚你想要 – Nitin

+0

你的意思是說你希望每行有一個圖像/標籤對..? – Chris

+0

是的,我需要一個pair.I使用兩個stackpanels.But了一個更多的疑問是我需要有一個MouseDoubleClick事件動態創建labels.Can任何人都可以幫助我嗎? – Suresh

回答

1

這裏是你的代碼應該看起來怎麼樣,使用數據綁定,一個ItemsControlDataTemplate

XAML

<Window x:Class="Ping.MainWindow" WindowStyle="ThreeDBorderWindow" Icon="F:\ChatApplication\Ping\Ping\Images\title.ico" Cursor="Pen" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Ping - Connected by alphabets" Height="450" Width="750" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" > 
<Grid Name="grid_window"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="200"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <GridSplitter HorizontalAlignment="Right" 
       VerticalAlignment="Stretch" 
       Grid.Column="1" ResizeBehavior="PreviousAndNext" 
       Width="5" Background="#FFBCBCBC"/> 

    <TabControl Grid.ColumnSpan="2" Name="tab_control" BorderBrush="Cornsilk" BorderThickness="4" HorizontalAlignment="Left" Height="419" Margin="227,0,0,0" VerticalAlignment="Top" Width="515"> 
     <TabItem Header="Home" BorderBrush="Green"/> 
    </TabControl> 

    <ItemsControl ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
        <DockPanel> 
         <!-- You really should add the image as a resource to the project --> 
         <Image Source="F:\ChatApplication\Ping\Ping\Images\visible.png" 
          Width="10" Height="10" /> 
         <TextBlock Text="{Binding}" /> 
        </DockPanel> 
       </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

C#

public MainWindow() 
{ 
    InitializeComponent(); 

    DBCoding dbobj = new DBCoding(); 
    List<string> online = dbobj.onlineUsers(); 
    DataContext = online; 
}