GKhedekar
GKhedekar

Reputation: 79

In WPF app the filepath is not showing from right end in textbox

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 :&#xD;&#xA;" 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>

Upvotes: 3

Views: 342

Answers (2)

Mimax
Mimax

Reputation: 11

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);
    }

Upvotes: 1

Rafal
Rafal

Reputation: 12619

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.

Upvotes: 1

Related Questions