0

OUtput from SSH client is added to Results ObservableCollection I get the info shown in the listview.

How can I bind a scrollable readonly textbox to list everything from the collection?

<!--<ListView ItemsSource="{Binding Results}" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="15">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Result" DisplayMemberBinding="{Binding}"/>
                </GridView>
            </ListView.View>
        </ListView>-->

I already have a convert if that is of any help

public class JoinStringsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var strings = value as IEnumerable<string>;
        return string.Join(Environment.NewLine, strings);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Hairydruidy
  • 107
  • 2
  • 11
  • That converter is not going to work with ObservableCollection – paparazzo Jan 21 '15 at 15:51
  • Also, might want to know that the convertor does trigger at initialisation, but not when new strings are added. – Hairydruidy Jan 21 '15 at 16:48
  • A valid question then? It is not clear what you want to do. The stated input is not compatible with that converter. – paparazzo Jan 21 '15 at 16:54
  • There is a valid question, if you ignore the convertor. I only thought i would need a converter as a solution so that is why I posted one. Just ignore that part. – Hairydruidy Jan 21 '15 at 19:28
  • I posted my own answer then. A MultiValueConverter with the stringcollection and the count property of said collection. So it will update once the collection gets changed. – Hairydruidy Jan 26 '15 at 09:33
  • 1
    The answer of Hairydruidy works, but you have to combine it with https://stackoverflow.com/a/12346543/7821336 (for `TextBoxUtilities.AlwaysScrollToEnd`). – mre May 23 '19 at 13:29
  • wow this is rather old :p I haven't worked with wpf / xaml since. I would hope there would be better libraries now that are handling these kind of things – Hairydruidy Jun 14 '19 at 18:46

1 Answers1

1

Converter

public class ObservableStringCollectionToMultiLineStringConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        ObservableCollection<string> logEntries = values[0] as ObservableCollection<string>;
        StringBuilder sb = new StringBuilder();
        if (logEntries != null && logEntries.Count > 0)
        {

            foreach (string msg in logEntries)
            {
                sb.AppendLine(msg);
            }

            return sb.ToString();
        }
        else
            return String.Empty;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Textbox element

<GroupBox Header="Log" Grid.Row="4" Margin="0,10,0,0">
        <TextBox Style="{StaticResource SelectableTextBlockLikeStyle}" VerticalScrollBarVisibility="Auto" ap:TextBoxUtilities.AlwaysScrollToEnd="True" AcceptsReturn="True">
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ObservableStringCollectionToMultiLineStringConverter}">
                    <Binding Path="IncomingMessages" Mode="OneWay"/>
                    <Binding Path="IncomingMessages.Count" Mode="OneWay" />
                </MultiBinding>
            </TextBox.Text>
        </TextBox>
    </GroupBox>
Hairydruidy
  • 107
  • 2
  • 11