![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | How do I create for example a dockpanel in xaml and add elements form the code-behind? Hi, If I create a dockpanel in my Window1.xaml like this: <Window x:Class="WindowsApplication8.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication8" Height="300" Width="300" > <DockPanel x:Name="myDockPanel"> </DockPanel> </Window> How shall I add for example a button to my dockpanel form my code behind (Window1.xaml.cs)? What I have tried in my code-behind. Button myButton = new Button(); myButton.Content = "Please Help"; DockPanel.SetDock(myButton, Dock.Top); myDockPanel.Children.Add(myButton); this.Content = myDockPanel; / Tommy |
My System Specs![]() |
| | #2 (permalink) |
| | RE: How do I create for example a dockpanel in xaml and add elements f You don't have to set the window's content to the dock panel again. So this line ( this.Content = myDockPanel; ) can be removed. Other than that, the sample should work fine. -- Valentin Iliescu [MVP - Client Application Development] "HokutoNoKen" wrote: > Hi, > > If I create a dockpanel in my Window1.xaml like this: > > <Window x:Class="WindowsApplication8.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="WindowsApplication8" Height="300" Width="300" > > > > <DockPanel x:Name="myDockPanel"> > > </DockPanel> > > </Window> > > How shall I add for example a button to my dockpanel form my code behind > (Window1.xaml.cs)? > > What I have tried in my code-behind. > > Button myButton = new Button(); > myButton.Content = "Please Help"; > > DockPanel.SetDock(myButton, Dock.Top); > myDockPanel.Children.Add(myButton); > this.Content = myDockPanel; > > / Tommy > > > > > > |
My System Specs![]() |
| | #3 (permalink) |
| | RE: How do I create for example a dockpanel in xaml and add elements f I forgot something, by default DockPanel has LastChildFill=true so it will disregard the Dock value. Set it to false to make the sample work. -- Valentin Iliescu [MVP - Client Application Development] "HokutoNoKen" wrote: > Hi, > > If I create a dockpanel in my Window1.xaml like this: > > <Window x:Class="WindowsApplication8.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="WindowsApplication8" Height="300" Width="300" > > > > <DockPanel x:Name="myDockPanel"> > > </DockPanel> > > </Window> > > How shall I add for example a button to my dockpanel form my code behind > (Window1.xaml.cs)? > > What I have tried in my code-behind. > > Button myButton = new Button(); > myButton.Content = "Please Help"; > > DockPanel.SetDock(myButton, Dock.Top); > myDockPanel.Children.Add(myButton); > this.Content = myDockPanel; > > / Tommy > > > > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How do I create for example a dockpanel in xaml and add elements form the code-behind? Add loaded, se below and the eventhandler Windowloaded in the code behind Window1.xaml : <Window x:Class="WindowsApplication8.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication10" Height="300" Width="300" Loaded="WindowLoaded" > <Window.Resources> <Ellipse x:Key="shape" Fill="Blue" Width="100" Height="80" /> </Window.Resources> <DockPanel x:Name="myDockPanel"></DockPanel> </Window> Window1.xaml.cs: private void WindowLoaded(object sender, RoutedEventArgs e) { // Add button Button myButton = new Button(); myButton.Content = "Click me!"; this.myDockPanel.Children.Add(myButton); Ellipse myEllipse = (Ellipse)this.FindResource("shape"); this.myDockPanel.Children.Add(myEllipse); } This also solve the question about resources ("Resources doesn´t work at all?"), this code should also be here (as you see in the example code above with the ellipse) Regards, Tommy "HokutoNoKen" <tommy.herceg@programgruppen.se> wrote in message news:enYGEXpaGHA.1352@TK2MSFTNGP05.phx.gbl... > Hi, > > If I create a dockpanel in my Window1.xaml like this: > > <Window x:Class="WindowsApplication8.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="WindowsApplication8" Height="300" Width="300" > > > > <DockPanel x:Name="myDockPanel"> > > </DockPanel> > > </Window> > > How shall I add for example a button to my dockpanel form my code behind > (Window1.xaml.cs)? > > What I have tried in my code-behind. > > Button myButton = new Button(); > myButton.Content = "Please Help"; > > DockPanel.SetDock(myButton, Dock.Top); > myDockPanel.Children.Add(myButton); > this.Content = myDockPanel; > > / Tommy > > > > > |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Form Elements - How To? | VB Script | |||
| Create a form that can be e-mailed | Vista account administration | |||
| Create a New Form | Vista General | |||
| Should New-Item consistently create needed path elements? | PowerShell | |||