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..
Related
When I use 125% DPI setting in Windows, sometimes a button border is not rendered. It seems to depend on the size and position, maybe also the parent element of a button:
In my app, I can also see in the WPF designer that a parent border seems to be too small in size, though the button itself is larger. Maybe the measure process is somehow wrong:
I tried changing UseLayoutRounding for the whole window, also changing SnapsToDevicePixels does not help.
DPI Awareness is set in the manifest.
Does anyone know how to fix this for the entire application?
Thats the code in a default Wpf Window Application (I can't reproduce the problem right now, but the problem persists in my application):
<Window x:Class="WpfApp3.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"
mc:Ignorable="d"
Title="MainWindow"
SizeToContent="WidthAndHeight">
<StackPanel>
<GroupBox>
<GroupBox.Header>
<Button Width="20"
Height="22"
Content="X" />
</GroupBox.Header>
</GroupBox>
<Button Width="20"
Height="22"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Content="X" />
</StackPanel>
</Window>
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);
}
how to set focus on this when i log in to my application ,when log in window will open i want to type the password on this without using mouse
<PasswordBox x:Name="passwordbox" Grid.Row="1" Grid.Column="1" Margin="5,35,5,5" Width="280" Height="27" app:PasswordBoxAssistant.BindPassword="true" app:PasswordBoxAssistant.BoundPassword="{Binding TechUserModel.UserId,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center">
I have tried various solutions for this scenario and the most effective that I have found is using FocusManager.FocusedElement:
<Window x:Class="StackOverflow.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300"
FocusManager.FocusedElement="{Binding ElementName=Box}">
<Grid>
<TextBox x:Name="Box"/>
</Grid>
</Window>
SOLUTION:
passwordbox.Focus();
or in .xaml file include the following in the element as its attributes.
FocusManager.FocusedElement="{Binding ElementName=passwordbox}">
NOTE:
If you want to decide which control to give focus to when the form first appears, put the code in the Loaded event.
Loaded += (o, e) => {
passwordbox.Focus();
};
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 window with this markup
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Button Command="ApplicationCommands.Close">Foo</Button>
</Window>
I'd like to button have MinWidth and MinHeight 100
and MaxHeight and MinHeight equals to 500
And like size window to content so I make this
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" >
<Button MaxHeight="500" MaxWidth="500" MinHeight="100" MinWidth="100" Command="ApplicationCommands.Close">Foo</Button>
</Window>
And I'd like to set initial size of button 200 x 200
without writing Width and Height in window
If I write Width="200" Height="200" in button, button becomes unresizable.
How can I set initial size of control?
I am not sure exactly what you are trying to accomplish, but see if this does what you are wanting. I am using a ViewBox to stretch the Contents when the Window is resized, and setting the contraints in the Window. Be aware that Window Size is different than Window Client Area size.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MaxHeight="500" MaxWidth="500"
MinWidth="100" MinHeight="100"
Width="200" Height="200">
<Viewbox StretchDirection="Both" Stretch="Fill">
<Button >Hello</Button>
</Viewbox>