I want to view the content of a textblock that was added last. It means I want to auto scroll to the end and view the hidden content when I add more text... just like in Windows calculator.
In the calculator when I enter more numbers it shows only the numbers entered last. Previously entered numbers are hidden when there is not enough space. I want to do the exact same thing..
Can someone please help me?
I don't think TextBlocks can scroll. You can put the TextBlock in a ScrollViewer.
XAML:
<ScrollViewer Name="MyScrollViewer">
<TextBlock TextWrapping="Wrap">
A bunch of text
</TextBlock>
</ScrollViewer>
Code-behind:
MyScrollViewer.ScrollToBottom();
It appears that if you have multiple TextBlocks in a ListBox, you cannot get access very easily to it's ScrollViewer to accomplish the same thing. If you are doing this, change your ListBox to an ItemsControl and put that into a ScrollViewer. I think you'll lose selection ability though.
If you do need to use a ListBox, then you can get the view that belongs to the last item and call the ListBox's ScrollIntoView() method. See this or this for a little bit about that, but you might have to do a little more research.
Do you mean a TextBox, as opposed to a TextBlock? The default behavior for a TextBox is to show the most recent text as more text is entered.
Window x:Class="textboxscrolltest.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">
<Grid>
<TextBox Width="75" Height="25"/>
</Grid>
</Window>
Related
I have a TabControl,inside one Tabitem, i have a grid and my userControl inside the grid:
<TabControl>
<TabItem>
<Grid HorizontalAlignment="Left" Height="64" Margin="288,150,0,0" VerticalAlignment="Top" Width="354">
<Canvas>
<local:MyCustomComboBox x:Name="ucc1" HorizontalAlignment="Left" Grid.RowSpan="2" Grid.ColumnSpan="3" Height="30" VerticalAlignment="Top" Width="194" ClipToBounds="True"/>
<Canvas>
</Grid>
<TabItem>
<TabControl>
By default,when the userControl's size is greater than the grid's/TabItem's size,the extra portion can't be seen.How can i make my UserControl overlap it ? I tried to add RowSpan and ColumnSpan but it didn't work :(
TabItem has it's own bounds which u cannot overlap.So,there's no way u can achieve your goal ... But i always try my best to help people, so here's a quick tip :
If the usercontrol is bigger than the gird
Your userControl XAML includes MyCustomComboBox, which makes me think it is a combobox..I've seen your previous post where you wanted to customize your combobox but couldn't quite achieve your goals...So, are you trying to create you own custom combobox and by usercontrol bigger than the grid , did u mean that the drop down menu u created doesn't go outside the grid rather is clipped to the grid ??
If this is the case , u can use a ContextMenu and move your custom drop-down list there.Then the contextMenu will overlap both the TabItem and Grid as it is a window itself.
Also , NOTE that u cannot use Named content in usercontrol(u can but that requires a workaround).So i suggest you to add all required code behinds , even set required binding from the user-control's code behind.
Hope this helps :)
Am trying to set a TextBox for multiline. However it is not working. When the Enter key is pressed it leaves the TextBox instead of new line. Checking online for solution, I have come across TextBox Enter does not work. I have tried the suggestions mentioned there but still it's not working. It was inside a ScrollViewer which I removed but still. I've also tried putting it inside a Canvas still no luck. I've seen where clipping mentioned but I have not been able to find a solution how to remove that. I have also make it as a UserControl and reference it but no luck. However if I create a new Window it works. The TextBox is inside a TabItem. Code Structure is similar to:
<Window x:Class="CSD_EMRS.Windows.Code"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Code" Height="300" Width="300">
<Grid>
<TabControl>
<TabItem>
<Grid>
<GroupBox>
<TextBox AcceptsReturn="True" TextWrapping="Wrap" />
</GroupBox>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
UPDATE: Sorry guys, am working on inherited code and just saw that there's a PreviewKeyDown code changing the behaviour.
I have a TabControl with TextBox controls in the ContentTemplate. When I type some text in one tab and switch to another tab, the Undo history in the original tab is gone when I go back.
Another problem that comes up is any text that was selected is deselected and the caret moves to the beginning of the TextBox.
If I make a window with just hardcoded TabItem controls, the undo history is preserved. The issue has something to do with my binding or templates.
Here is my XAML for the main window
<Window x:Class="TabbedTextAreaTest.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Command="{Binding AddNewTab}">Add Tab</Button>
<TabControl ItemsSource="{Binding Tabs}" Grid.Row="1">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
Is there a way to preserve the undo/redo history and selected text when switching tabs without manually catching those commands?
When you use a TabControl which gets its tabs via databinding on ItemsSource, WPF doesn't keep the visual tree for each item around as you switch. Thus, when you switch from tab 1 to tab 2, then back to tab 1, the controls on tab 1 are not actually the same control instances which you saw on tab 1 the first time.
There are a number of ways around to deal with this - TabControls which have explicit TabItem instances do keep their visual trees when you switch tabs, so probably the easiest way to do it is to wrap your collection of tab items in something which makes TabItems for them.
Unfortunately right now I can't find a link to an example of how to do this. There are references to articles elsewhere on SO, but they all seem to point to pages which no longer exist, and I don't have time to dig any deeper.
The reason is simple. If you think the both operation you complain about are strictly UI operations: Undo: user editing on the UI control, selection: selection of the text on UI control.
When you swicth Tab to another and go back, what happens in WPF is that all controls are rebinded to there data (ModelView normally, or just Model) again, as if you was showing them for the first time. So they loose their UI appearance attributes.
To manage that correctly in Tab environment in WPF you need to manage Undo/Redo stack by your own.
Good luck.
I am new to WPF and xaml and I have a problem with my apps UI.
I am using this xaml code:
<ScrollViewer HorizontalAlignment="Left" Margin="252,12,0,0" Name="captchaControlsScrollableContainer" VerticalAlignment="Top">
<Grid Name="captchaControls" Width="339" Height="286">
</Grid>
</ScrollViewer>
And this code behind code that populates the grid:
captchaControls.Children.Add(new Captcha(data));
which is called more than one time
My problem is that only the first user control app apperas in the grid although in the debugger captchaControls.Children.Count is the right size and the scrollviewer's scrollbar is disabled.
Does anyone have any idea what I am doing wrong? Thank you in advance.
Your Grid in the scrollviewer is set to have 1 column and 1 row.So you will see only the last one you add so far (all others controls are "below" the last).
Take a look to the StackPanel control and maybe this tutorial will be useful.
I want a TextBox with line numbers. So I decided to use one small TextBox for the line numbers on the left and another big one on the right for the text.
My problem now is that I put these two TextBoxes into a Dockpanel and I need a height difference because the TextBox for the line numbers should not have scrollbars. So I need to shorten the left TextBox. My plan is to put an empty StackPanel below the left TextBox. And I'm getting trouble because the DockPanel doesn't sort my controls like I want. The only way I got it was using a fix width but I don't want that!
Or should I go a complete different way?
I don't know why you have to build this control, but you can find something similar for WPF. See this link AvalonEdit. It's a text editor control.
If you don't want a scroll bar on a control, just set the VerticalScrollBarVisibility to disabled.
But I'm not sure that's exactly what you need. If you do this then obviously your line numbers aren't going to scroll with your text box. You best bet might be to put your two textboxes (although if the line numbers aren't supposed to be editable, you might want to use labels instead) in a dockpanel and wrap the dock panel in a scrollviewer.
You may try to use ScrollView. The code below demonstrates the idea. But I haven't come up with a solution to enable horizontal scrolling.
<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">
<Grid>
<ScrollViewer Height="100">
<DockPanel>
<TextBlock DockPanel.Dock="Left">
<TextBlock.Inlines>
1<LineBreak/>
2<LineBreak/>
3<LineBreak/>
4<LineBreak/>
5<LineBreak/>
6<LineBreak/>
7<LineBreak/>
8<LineBreak/>
9<LineBreak/>
10<LineBreak/>
11<LineBreak/>
12<LineBreak/>
13<LineBreak/>
</TextBlock.Inlines>
</TextBlock>
<TextBox AcceptsReturn="True" TextWrapping="Wrap">
I want a TextBox with line numbers. So I decided to use one small TextBox for the line numbers on the left and another big one on the rigth for the text. My problem now is that I put these two TextBoxes into a Dockpanel and I need a Heigth difference because the TextBox for the line numbers should not have scrollbars. So I need to short the left TextBox. My plan is to put an empty StackPanel below the left TextBox. And I'm getting trouble because the DockPanel doesen't sort my controls like i want. The only way I got it was using a fix width but I don't want that!
</TextBox>
</DockPanel>
</ScrollViewer>
</Grid>
</Window>
It looks like