wpf textbox cursor scrolling - wpf

What property do I need to set so that the text near the cursor within a WPF textbox is always visible. I have to set the width of a one line textbox and once I type outside of the width I can't see what I'm typing I can set ScrollViewer.CanContentScroll = True but that makes the height increase, which is better than the former but I would prefer the text near the cursor to be visible like common WinForms textbox behavior.
Any ideas? I figured a WPF guru or maybe even novice could answer this question faster than it would take me to experiment/google
Thanks in advance

The behavior you are describing should be how it operates by default. To test this I just created a TextBox...
<TextBox Width="50" Height="22" ></TextBox>
...began typing and the most recent character I typed is where the cursor was living and therefore what was visible.
Perhaps you have a style or something applied that is negating this default behavior.

Aaron McIver's answer points at the styles, and OP comments that their global style setting TextWrapping="WrapWithOverflow" was their problem. I had a similar problem but with a different solution. In my case the problem was that the style of ScrollViewer had just
<ScrollContentPresenter Margin="1" />
for the ScrollContentPresenter. The default template has
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0" />
Of the missing properties, the really important one is CanContentScroll.

Related

Wpf, AvalonEdit and keyboard navigation problem

I have an AvalonEdit on my window. When I press key combination Ctrl+Up or Ctrl+Down when inside editor, AvalonEdit loses focus, which is transferred to a different control, as below:
This sometimes happen as well when using Ctrl+Left or Ctrl+Right combinations.
My current XAML definition looks like following:
<ae:TextEditor x:Name="teEditor"
Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
BorderThickness="0"
FontFamily="Consolas"
FontSize="10pt"
TabIndex="0"
WordWrap="{Binding ElementName=Root, Path=Handler.WordWrap}"
ShowLineNumbers="{Binding ElementName=Root, Path=Handler.LineNumbers}"
ContextMenu="{StaticResource EditorContextMenu}"
GotFocus="HandleEditorGotFocus"
KeyboardNavigation.ControlTabNavigation="None"
KeyboardNavigation.AcceptsReturn="True"
KeyboardNavigation.DirectionalNavigation="None"
KeyboardNavigation.TabNavigation="None"/>
How can I prevent that?
It turns out, that problem appears, when you place AvalonEdit inside TabControl. In such case you have to disable keyboard navigation on the TabControl by adding:
KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.DirectionalNavigation="Contained"

XAML Property Element Not Found in Parent, what does it mean?

I have been drilling through XAML and trying to puzzle together how it actually works.
I have got the following XAML from one of the sample code I downloaded, an earlier question has explained away a large part of my confusion, however... I am still trying to make sense why the following element has a child element that references another type altogether.
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<VisualStateManager.VisualStateGroups>
<!-- Snipped code irrelevant - just various storyboards-->
</VisualStateManager.VisualStateGroups>
<Grid x:Name="InnerGrid"
Opacity="1"
Margin="0,5,0,5"
Background="{StaticResource TransparentColor}">
<ContentPresenter x:Name="ContentPresenter"
Foreground="{StaticResource TransparentColor}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</Border>
Looking at the XAML syntax documentation, the VisualStateManager is a PropertyElement, however I am confused because VisualStateGroups do not appear to be a property of Border. I believe the example is correct however, I need someone to explain to me, how is an element that's not a "proper" Child element (as that's what Grid is), be a legit part of the parent element?
VisualStateManager.VisualStateGroups is attached property. Read the following topic, it may help you: http://msdn.microsoft.com/en-us/library/ms749011.aspx

WPF Textbox FlowDirection + HorizontalContentAlignment issue

I have a Control template targeting the ComboBox (TargetType="{x:Type ComboBox}")
In this template is a TextBox:
<TextBox x:Name="PART_EditableTextBox"
FlowDirection="RightToLeft"
HorizontalContentAlignment="Left"
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
/>
My initial issue was the text in the TextBox was cut off at the front of the sentence and showing the end of the sentence.
That is when I added the flow direction in, it solved my original issue but created a new one.
In the TextBox, short text is now Right aligned and no longer left aligned.
Is there an issue setting both FlowDirection + HorizontalContentAlignment in wpf TextBoxes?
and if so, is there a work around?
Try setting HorizontalAlignment="Left" for the TextBox. This way short text will be left aligned as well
<TextBox x:Name="PART_EditableTextBox"
FlowDirection="RightToLeft"
HorizontalAlignment="Left"
... />
Try
<TextBox x:Name="PART_EditableTextBox"
FlowDirection="RightToLeft"
TextAlignment="Right"
... />
Since, FlowDirection is changed we need to set TestAlignment to 'Right' instead of 'Left'

ContentPresenter.ContentSource vs Content

Basically, I don't understand what the real difference here is:
The Microsoft code for TabItem uses:
<ContentPresenter ContentSource="Header" ... />
So, when would one use the Content property instead of (or in addition to) ContentSource?
This property should only be used when
the ContentPresenter is in a template.
When a template contains a
ContentPresenter with ContentSource
set to "Abc", the Content,
ContentTemplate, and
ContentTemplateSelector properties of
the ContentPresenter are automatically
aliased to Abc, AbcTemplate, and
AbcTemplateSelector, respectively.
Beginning with the .NET Framework
version 3.5 Service Pack 1, setting
ContentSource to "Abc" also causes the
ContentStringFormat property to be
aliased to AbcStringFormat.
The two most useful values for this
property are "Content" and "Header".
(MSDN)
ContentSource apparently sets more properties at once for convenience.
Practically, The declaration:
<ContentPresenter ContentSource="Header" />
Performs the following initialization.
<ContentPresenter Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
ContentStringFormat="{TemplateBinding HeaderStringFormat}" />
It does this for each property separately only if the dependency property exists on the templated control.

How to make a plain text-only Silverlight button?

I simply want a button with no background or anything other than plain text. I have done the following and the button does not show up at all:
<UserControl.Resources>
<ControlTemplate x:Key="linkButtons" TargetType="Button">
<TextBlock Foreground="White" FontSize="28" FontFamily="Verdana" Padding="10"></TextBlock>
</ControlTemplate>
</UserControl.Resources>
<Button Template="{StaticResource linkButtons}" Content="Hello World!"/>
This is because the TextBlock inside the Control template does not have a template binding. Make an attribute like this:
<TextBlock Foreground="White" FontSize="28" FontFamily="Verdana" Padding="10" Text="{TemplateBinding Content}" />
Not sure if thats the correct syntax, but thats the concept.
The problem is that a button is designed to have content, not text - it's a kind of ContentControl. So, to display the content, your template should have this in it:
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
OR, you could make a custom control based on button, add a Text property to it, use your current TextBlock control in the template (but with Text="{Binding Text}") and leave the ContentPresenter out of your template. Making a custom control is a little trickier than just making a template for an existing one, but it's really the best way to get exactly what you're going for.

Resources