How do I copy WrapPanel?
I have window in which I have WrapPanel with some controls inside. I'd like to copy it inside my code so I would have 2 or more exact looking panels on the same window but with different names for controls.
This is how my XAML looks like:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel x:Name="stackPanel" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="498">
<WrapPanel x:Name="Wrap1" Height="142" Margin="0,0,345.6,0" VerticalAlignment="Top" Width="152">
<Button x:Name="wrap1_button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Button x:Name="wrap1_button1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Label x:Name="wrap1_label" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Width="74"/>
</WrapPanel>
</StackPanel>
</Grid>
I would like to get another WrapPanel placed next to first one, named Wrap2 with controls inside wrap2_button, wrap2_button1, wrap2_label.
I tried inserting it in StackPanel and doing something like that:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WrapPanel Wrap2 = Wrap1;
stackPanel.Children.Add(Wrap2);
}
}
but it doesnt work...
In short, you will have to create another WrapPanel, add it to the StackPanel, and add children to the second WrapPanel.
Now, if your reaction is to recoil from the duplication, that is a good instinct. This may be a good use case for a UserControl which is a way to bundle groups of standard WPF controls to allow for their reuse.
What I would suggest is to create a new UserControl which is a WrapPanel with the children you have, then add 2 instances of that new UserControl to the StackPanel in your window.
Here is a link to the Remarks of the UserControl class with some of the considerations of creating your own custom controls.
Related
I have a textbox in WPF window to show the path of file and browse button beside the textbox, I am selecting a file from (tfs) browser and I want to show the full path aligned from right side but it is showing from left side only.
The Image show the path showed from left
I tried HorizontalAlignment,VerticalAlignment,HorizontalContentAlignment but nothing is helping.
I want to display it from right end.
I have referred
Show right end of text line in TextBox
TextBox.TextAlign right-side alignment has no effect in certain conditions?
Can anyone Help?
Here is the code
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label x:Name="label" Content="Source :
" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.144,1.871" Margin="10,27,0,0"/>
<TextBox x:Name="txtSourceFile" HorizontalAlignment="Left" Height="23" Margin="67,34,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="400" TextAlignment="Justify" HorizontalContentAlignment="Right" />
<Button x:Name="btnBrowseSource" Content="Browse" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="379,62,0,0" Click="btnBrowseSource_Click" />
</Grid>
</Window>
OK, so its not that trivial as setting a property on text box what you can do is declare this method:
private void ScrollToEnd()
{
var sv = (ScrollViewer)txtSourceFile.Template.FindName("PART_ContentHost", txtSourceFile);
sv.ScrollToHorizontalOffset(sv.ExtentWidth);
}
And call it whenever your textbox value is changed.
I had the same problem and added a method for the Loaded and TextChanged event
<TextBox Loaded="FolderTB_ScrolltoEnd" TextChanged="FolderTB_ScrolltoEnd" ... />
and for setting the offset
private void FolderTB_ScrolltoEnd(object sender, EventArgs e)
{
((TextBox)sender).ScrollToHorizontalOffset(((TextBox)sender).ExtentWidth);
}
I've added a user control (with labels and textboxes) to my WPF project shown here:
<UserControl x:Class="MyProject.myControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="384" Width="543">
<Grid x:Name="_LabelServerURL">
<Label x:Name="_LabelServerUrl" Content="Server Url:" HorizontalAlignment="Left" Margin="60,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<TextBox x:Name="_TextboxUrl" HorizontalAlignment="Left" Height="23" Margin="158,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Label x:Name="_LabelURLExample" Content="(https://promodel.com)" HorizontalAlignment="Left" Margin="398,21,0,0" VerticalAlignment="Top" Width="139"/>
</Grid>
</UserControl>
and have created an instance of that user control in my main window code behind.
public partial class MainWindow : Window
{
private myControl _settings = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
_ButtonEPSSettings.ToolTip = _epsSettings;
//SetButtonTootips();
}
In my main window I have a list of buttons, and I want to set their tooltip property (or bind the tooltip property) to the instance of my user control so that the tooltip dynamically updates when I add values to the textboxes in my user control. So far in my main window xaml I've been able to show my user control as the tooltip shown here:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls ="clr-namespace:MyProject" <!--Declare project namespace-->
Title="EPS Configuration Manager" Height="415" Width="766">
<Grid>
<Button x:Name="_ButtonSettings"
Content="EPS Settings"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="199"
HorizontalContentAlignment="Left"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="_ButtonSettings_Click"
>
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
I am still not clear on setting that tooltip as the instance of my user control as declared in my main window code behind. Any help will go a long way. Thanks.
If you set tooltips in backgroud, don't use these code:
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
If you want the tooltip dynamically updates:
private myControl mytooltip = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
mytooltip._TextboxUrl.Text = "Hello";
_ButtonEPSSettings.ToolTip = mytooltip;
//SetButtonTootips();
}
In my example wpf app I've added one button and one popup to the window. The button is in the bottom right corner and the popup has set "PlacementTarget" property to it and "Placement" set to top. The popup consists of one very long textblock.
What I expect this popup will behave is not to go outside of the window and therefore automatically set his "HorizontalOffset" to the appropriate value, but the popup behaves against my intentions.
Here's my xaml file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Name="window" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Converters x:Key="Converters"/>
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" VerticalAlignment="Bottom" Width="75" HorizontalAlignment="Right"/>
<Popup Placement="Top" PlacementTarget="{Binding ElementName=button, Mode=OneWay}" IsOpen="True">
<TextBlock TextWrapping="Wrap" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Background="White"/>
</Popup>
</Grid>
Do anyone know how to fix it?
I've read that this should be default popup behavior to take care of going out of the boundaries, but not in my case. Thanks in advance.
Have you tried to set the width of the Popup or Textblock ?
Sorry, I can't write this poor answer as a comment..
I am new to WPF and i can not figure some things out. I just started an new project and i wanted to make a StackPanel because i saw that on a tutorial. But now i've implemented the StackPanel and i get 2 errors.
The object 'Window' already has a child and cannot add 'StackPanel'. 'Window' can accept only one child. Line 9 Position 116.
The property 'Content' is set more than once.
Can someone explain to me what i am doeing wrong.
This is my code:
<Window x:Class="CheckDatabase.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckDatabase" Height="350" Width="525">
<Grid Margin="10,80,10,10" >
<TextBox TextWrapping="Wrap"/>
</Grid>
<StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Name="ButtonPanel" VerticalAlignment="Top">
<Button Margin="0,10,0,10">Button 1</Button>
<Button Margin="0,10,0,10">Button 2</Button>
</StackPanel>
Thanks in advance
A Window can only contain one child. However, your Window contains both a Grid and a StackPanel.
To fix this you need to put the StackPanel inside the grid (if that is the intention) or wrap both the Grid and the StackPanel inside another panel that positions the two elements in the way you want.
Some Controls like Window can only have a single child. You will have to remove the Grid or either nest another Grid arround your Grid and Stackpanel.
Example:
<Grid x:Name="outerGrid">
<Grid x:Name="innerGrid"></Grid>
<StackPanel x:Name="innerStackPanel></StackPanel>
</Grid>
Window is a ContentControl and hence can have only one Content. You can do the following to have the expected layout
<Window x:Class="CheckDatabase.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckDatabase" Height="350" Width="525">
<StackPanel>
<Grid Margin="10,80,10,10" >
<TextBox TextWrapping="Wrap"/>
</Grid>
<StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Name="ButtonPanel" VerticalAlignment="Top">
<Button Margin="0,10,0,10">Button 1</Button>
<Button Margin="0,10,0,10">Button 2</Button>
</StackPanel>
</StackPanel>
I have a simple window with a simple composite control embedded within it.
(Main Window)
<Window x:Class="TabOrder.Window1"
xmlns:local="clr-namespace:TabOrder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label>
<TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label>
<TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
<local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/>
</Grid>
(Composite control)
<UserControl x:Class="TabOrder.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label>
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label>
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
</Grid>
As expected on my form I get 4 text boxes...
First
Second
Third
Fourth
But when "First" has focus and I hit tab the focus is switched to "Third". WPF seems to be seeing the tab list as a single flat list rather than as a tree where MyControl is TabIndex 3 and the text box "Third" the first tabbed control within it.
Is this a bug in WPF or is there another way of doing this? The composite control is used in many windows, it could even be used more than once on a single window.
I know this response is quite late... but have you tried:
<UserControl ... KeyboardNavigation.TabNavigation="Local">
Doing so will ensure once your UserControl has recieved focus, you will navigate only through TabStop within your UserControl (instead of worring about conflicting TabIndex values throughout your app). After looping through the TabStops of your UserControl, TabNavigation will resume to the TabStop outside of it.
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx