WPF DockPanel - order controls - c#

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

Related

Add textblock to bottom of listview

I need to put a textblock containing result count information at the bottom of a databound listview. It needs to be within the listview's scrollbars but not be affected by the scrollbars (it must always sit at the bottom of the listview).
Because a listview cannot contain a textblock directly I am achieving this by adding some padding at the bottom of the listview and using a negative margin to make a separate textblock appear as though it is part of the listview. The issue with this is that when the listview's horizontal scrollbar is displayed it covers the textblock. I could add code to figure out if the scrollviewer is displayed and then adjust the margins/paddings accordingly BUT at this stage it sounds like a hacky solution.
Is there a better way to achieve this?
Instead of using a ListView.. you can do something like this:
<ScrollViewer>
<StackPanel>
<ItemsControl ItemsSource="{Binding MyCollection}" />
<TextBlock />
</StackPanel>
</ScrollViewer>

Auto scroll to the end of a Textblock in WPF

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>

How to hide part of text in a richtextbox or textbox using WPF?

i would like to ask for a a way in wpf of hiding and unhiding some specific lines in a richtextbox or textbox using C# at Runtime and at the same time leave the rest of the lines visible. I would also like not to be a visible space between the visible and unvisible lines. i have an idea of selecting the lines and then change font size to 0.01, but it isnt so much elegand.
thanks in advance.
By default, you can only hide and show certain text in a WPF TextBox or RichTextBox by changing the value of the Text or RichText properties respectively. For example if you have the text "Mary had a little lamb who's fleece was white as snow" and wanted to hide the text "who's fleece was white as snow" conditonaly you would have to parse the text and remove or add "who's fleece was white as snow" in the correct location in the Text or RichText using code.
It sounds like you are just trying to modify the layout depending on certain conditions though. In that case, it is better to split the lines up into separate TextBoxes and set the visibility of those textboxes to Collapsed when certain conditions exist and set them to Visible otherwise.
If you are using TextBox you will want an outer Border which will help you match the style of TextBox, then you can set the BorderBrush and Background of the textboxes you are hiding to Transparent so it just looks like a single TextBox.
<Border
BorderThickness="1"
BorderBrush="Black"
Background="White">
<StackPanel>
<TextBox
Text="Mary had a little lamb "
BorderBrush="Transparent"
Background="Transparent" />
<!-- Set the Visisibility Property of this TextBox in code to show or hide it -->
<TextBox
Name="ConditionalTextBoxLine"
Visibility="Visible"
Text="Who's fleece was white as snow"
BorderBrush="Transparent"
Background="Transparent" />
</StackPanel>
</Border>

Grid inside a scrollviewer with multiple user controls inside

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.

Custom Control or Slider like Control

What i am basically looking out for is a combination of controls that work as 1 whole. I have no idea what the best way would be to start solving this problem in WPF, either a custom control, existing control, slider...?
Only thing i do not want are 3th party controls and the such.
When a certain condition is met a button with text will be placed inside the slider. Every time when certain conditions are met this situation will keep on happening and buttons will be placed inside the border field.
So it could be possible i have like 10 buttons after each other inside the border. The 2 navigation buttons to the left and right serve as navigation between all those buttons so all can actually get view and pressed when needed for further actions.
Picture that illustrates what i wish to achieve:
You can start with something simple as a StackPanel (you dont need to put it in a page but i wanted to make it copy paste friendly):
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Button Content="Left"/>
<ListBox>
<ListBoxItem>btnTest 10:00h</ListBoxItem>
<ListBoxItem>btnTest 11:00h</ListBoxItem>
</ListBox>
<Button Content="Right"/>
</StackPanel>
</Page>
Then focus on the appearance and behavior separately.
You need to learn WPF styles, so you can get the colors and layout as in your sample picture. This will also let you make the ListBox horizontal.
Look into the concept of a ViewModel to learn how to populate the ListBox with items. And event handlers for the buttons.
Its a very broad question but I hope this gives you a start.

Categories

Resources