Search code examples
c#wpftextboxsplitseparator

Separate text in a Textbox with a drawn line?


So here it goes, I making a c# project in wpf and im stuck at trying to make a text box with a line that separates text.

At the moment i got the textbox like this:

enter image description here

Instead of using "-------" i want to make a predefined line in the textbox. Is this possible ?

Agg. The textbox is editable in runtime

The drawn line should have the properties:

  • It should not be edit able !
  • It shouldn't only be an empty line
  • It should be a visible line that has the width line.width = box.width !

Solution

  • If you don't need a full textbox implementation then this might help. It's got real issues with not showing the cursor ect but might give you a start.

    enter image description here

    first add the following converter to your project.

    public class TextLineConverter : MarkupExtension, IValueConverter
    {
        static TextLineConverter converter;
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string[] results = null;
            string newText = value as string;
            if (newText != null)
            {
                results = newText.Split('\r');
                if (results.Length > 0)
                    for (int i = 0; i < results.Length; i++)
                        if (results[i].Length > 0)
                            if (results[i][0] == '\n')
                                results[i] = results[i].Substring(1, results[i].Length - 1);
            }
            return results;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new TextLineConverter();
            return converter;
        }
    
        public TextLineConverter()
        {
        }
    }
    

    And the following style.

    <Style TargetType="TextBox">
                <Style.Resources>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Margin" Value="0"/>
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="IsEnabled" Value="False"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Border BorderThickness="0,0,0,2" BorderBrush="Black" >
                                    <ContentPresenter Content="{Binding}" />
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Style.Resources>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TextBox">
                            <ListView Focusable="False" ItemsSource="{Binding Text, Converter={local:TextLineConverter}, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>