This issue only happens on vista and xp. What is happening is if there is a textbox that has a lot of text and is partially off the screen and you click in it, wpf will scroll it into view and highlight text while it does this. In windows 7 it won't scroll it into view. I am using .net 4 and have tried clearing the selection on the textbox's received focus, got keyboard focus, and mouse capture events, but the scroll seems to take place after those. I have included some screen shots of what I am talking about as well as a test app that demonstrates the problem.
Before clicking on anything, In the next screen shot all I have done was click on line 6
After clicking on line 6 you can see everything is highlighted as it scrolled the textbox into view.
repro:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 1; i < 1000; i++)
{
textBox3.AppendText(string.Format("line {0}\r\n", i));
}
}
}
<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" Height="350" Width="525" MaxHeight="350" MaxWidth="525">
<Grid>
<ScrollViewer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="this is some text"></TextBox>
<TextBox Grid.Row="1" Text="this is some text"></TextBox>
<TextBox Grid.Row="2" ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" AcceptsReturn="True"
x:Name="textBox3"></TextBox>
</Grid>
</ScrollViewer>
</Grid>
</Window>
Turns out from Microsoft that this is a known issue in .net 3.5 and they fixed in .net 4.0. they did not have a work around.
Related
I am working on a Digital Signage project using Windows IoT core. So far I managed to design the grid and scroll images and videos. Now I have added a text block at the bottom of the grid and text in it should continuously scroll from left to right
For more detail example I need a MARQUEE of text in the bottom side.
Main page XAML code:
<Page
x:Class="Digital_Notiec_Board_V1._2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Digital_Notiec_Board_V1._2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#FF222222" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition MaxHeight="1800"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="imageInstance" Visibility="Collapsed" Grid.Row="0"/>
<MediaElement x:Name="audioInstance" Visibility="Collapsed" Grid.Row="0"/>
<MediaElement x:Name="videoInstance" Visibility="Collapsed" Grid.Row="0"/>
<ScrollViewer Grid.Row="1" Background="#FF5A80FF">
<TextBlock x:Name="ScrollText" TextWrapping="Wrap" Foreground="White" Text="AJ Y" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Auto" SelectionHighlightColor="#FF2D5DFF">
</TextBlock>
</ScrollViewer>
</Grid>
Well if you need to show a Marquee text only then I would advise you to use a custom control that allows you to put a text as a marquee.
Luckily, there is a GitHub repo for the same. You can check it out on `GitHub at MarqueeTextControl
works perfectly fine, I just tried it. If you need any further help, please use the comments section.
I'm having a problem with determining which element is responsible for a size misalignment. Here is a picture to illustrate it: click here to see it
The light grey area is the element size which I am trying to change, but cannot. The overall area is a User Control, the area inside the Tab is a WPF (as an element Host). I've tried changing both, but with no effect on the grey area (the other tabs are also affected by this). Here is the code for the hosted element (WPF):
<UserControl x:Class="SlideAnalyzer.DisplayAnalysis"
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"
xmlns:local="clr-namespace:SlideAnalyzer"
mc:Ignorable="d"
d:DesignHeight="150.8" d:DesignWidth="201.2">
<Grid Name="displayGrid" Height="64" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="160" Margin="10,0,31.6,9.4" RenderTransformOrigin="0.496,0.94">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Expander x:Name="expander" Grid.Row="0" Header="Titel zu lang" HorizontalAlignment="Left" VerticalAlignment="Top" Width="180" Background="Red" Margin="-10,-56,0,0">
<Grid Background="AliceBlue">
<TextBlock TextWrapping="Wrap"
Margin="5"><Run Text="This is some text content."/></TextBlock>
</Grid>
</Expander>
</Grid>
The host itself is a normal User Control. I cannot seem to get the right size of the hosted element so that the preview really reflects reality (because when I run the Add-In, the size of the WPF differs)
I tried changing the Properties of the UserControl and I found that AutoSize has a big impact when True (by default False)
Thanks!
I am using a Grid to balance 3 parts. The first two shall take up each 50% of the remaining space, the last two shall stay the same height at the bottom, as it is a bar with buttons.
I use a GridSplitter to allow dynamic resizing between the first two elements, but removing it doesnt change anything either.
Here is a minimal working example:
XAML:
<Window x:Class="TestWPFApplication.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:TestWPFApplication"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="rootGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox
x:Name="firstBox"
Grid.Row="0"
AcceptsReturn="True"
AcceptsTab="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>
<GridSplitter x:Name="splitter" Grid.Row="1" Height="5" HorizontalAlignment="Stretch" />
<TextBox
x:Name="secondBox"
Grid.Row="2"
AcceptsReturn="True"
AcceptsTab="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>
<Button Grid.Row="3" Content="Collapse" Click="Button_Click"/>
</Grid>
</Window>
CS:
namespace TestWPFApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
splitter.Visibility = Visibility.Collapsed;
secondBox.Visibility = Visibility.Collapsed;
}
}
}
After clicking the button, the second textbox and the gridsplitter collapse, so I cannot use them anymore (desired), but the remaining textbox does not take up the rest of the space (undesired).
In my understanding as the row has a star for height, it should automatically adjust its space. Do I need to call a function like updateUI or something so that the first textbox gets automatically resized?
You observe such behaviour, cause the heights of first and third grid row is set to '*'. After setting 2nd textBoxVisibility to collapsed 3rd grid row is not disappears. So if you write in Button_Click event handler something like this, all should work like expected.
rootGrid.RowDefinitions[2].MaxHeight = 0;
I can suggest to use DataBinding for it property to achieve this. You can bind second TextBox Visibility property to third RowDefinition MaxHeight property with ValueConverter. But I don't know the restrictions of your project.
I have created a basic application using Prism & MVVM. So far, it only consists of the Shell, and one View/ViewModel.
During application load, I am loading the View into my main region and this displays on screen. This works, but I cannot get the textbox on the view to focus. It looks like the cursor is in the box (although it's not flashing), but it doesn't accept text input until I click on the textbox.
I've recreated this in a new project, where all I've done is install prism/prism.unityextensions, set up the shell and the view, and loaded the view into the shell region. Neither xaml file has anything in the code behind.
Shell
<Window x:Class="MVVMFocusTest.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://www.codeplex.com/prism"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DockPanel LastChildFill="True">
<ContentControl Name="MainRegion" DockPanel.Dock="Top" prism:RegionManager.RegionName="MainRegion" />
</DockPanel>
</Grid>
</Window>
View1
<UserControl x:Class="MVVMFocusTest.View1"
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="300" d:DesignWidth="300">
<StackPanel>
<Grid FocusManager.FocusedElement="{Binding ElementName=Username}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0">Username</Label>
<TextBox Name="Username" Grid.Row="0" Grid.Column="1" ToolTip="Enter Username" TabIndex="0" />
<Label Grid.Row="1" Grid.Column="0">Password</Label>
<PasswordBox Grid.Row="1" Grid.Column="1" Name="LoginPassword" PasswordChar="*" ToolTip="Enter Password" TabIndex="1" />
</Grid>
</StackPanel>
</UserControl>
Can anyone point out what I'm doing wrong? As far as I'm aware, the FocusManager.FocusedElement="{Binding ElementName=Username}" should be sufficient to set the focus.
As per FocusManager documentation -
Logical focus pertains to the FocusManager.FocusedElement within a
specific focus scope.
So, its not necessary that element with logical focus will have keyboard focus as well but vice versa is true i.e. element with keyboard focus will surely have a logical focus as well.
As stated in documentation FocusManager.FocusedElement guarantees logical focus and not keyboard focus. So what you can do is create an attach behaviour similar to FocusManager.FocusedElement which will set keyboard focus on an element.
You can refer to this for setting keyboard focus using attached behaviour - Setting keyboard focus in WPF.
Code from that article -
namespace Invoices.Client.Wpf.Behaviors
{
using System.Windows;
using System.Windows.Input;
public static class KeyboardFocus
{
public static readonly DependencyProperty OnProperty;
public static void SetOn(UIElement element, FrameworkElement value)
{
element.SetValue(OnProperty, value);
}
public static FrameworkElement GetOn(UIElement element)
{
return (FrameworkElement)element.GetValue(OnProperty);
}
static KeyboardFocus()
{
OnProperty = DependencyProperty.RegisterAttached("On", typeof(FrameworkElement), typeof(KeyboardFocus), new PropertyMetadata(OnSetCallback));
}
private static void OnSetCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var frameworkElement = (FrameworkElement)dependencyObject;
var target = GetOn(frameworkElement);
if (target == null)
return;
frameworkElement.Loaded += (s, e) => Keyboard.Focus(target);
}
}
}
Use in XAML -
<UserControl xmlns:behaviors="clr-namespace:Invoices.Client.Wpf.Behaviors">
<Grid behaviors:KeyboardFocus.On="{Binding ElementName=TextBoxToFocus}">
<TextBox x:Name="TextBoxToFocus" />
</Grid>
</UserControl>
FocusManager.FocusedElement="{Binding ElementName=Username}" sets logical focus but not physical focus.
Physical focus is the normal focus, logical focus is kinda a second focus which is still a little bit buggy in wpf 4.0.
I would suggest you to use Keyboard.Focus(this.Username).
How can I get the height of a Page?, i.e; the height of all elements in the page.
For example, if I have this XAML which makes a Page, how can I get the height at runtime?
<Page x:Class="DialogViews.SomeError"
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="300" d:DesignWidth="400"
Title="SomeMessage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Style="{StaticResource ResourceKey=DialogTitleError}">A Problem</Label>
<TextBlock Grid.Row="1" Style="{StaticResource ResourceKey=DialogMsg}">
Some message to user
</TextBlock>
<Label Grid.Row="2" HorizontalAlignment="Right">
<Hyperlink>
Some link for info
</Hyperlink>
</Label>
</Grid>
</Page>
Reason:
I've created a custom Window that will display as a Dialog message to the user, the Window contains a Frame which in-turn contains this Page. I'm trying to fix the height of the Window to that of the Page.
I have tried:
myPage.Height; //NaN
myPage.ActualHeight; //0
If you want to fit the size of the Window to your user control Page, then I would suggest trying the SizeToContent property of Window.
<Window x:Class "MyNameSpace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="Height" />
Height - Specifies that a window will automatically set its height to fit the height of its content, but not the width.
Edit: This will stretch your Window to fit the Frame, which hopefully is set to change with the widths/heights of your Page.
Are you looking for
SystemParameters.FixedFrameVerticalBorderWidth
SystemParameters.FixedFrameHorizontalBorderHeight
SystemParameters.WindowCaptionHeight
Please check this msdn link
Try with
this.Height;<br/>
this.Width;