【问题标题】:ObservableCollection<string> bind textbox (scrollable) wpf c#ObservableCollection<string> 绑定文本框(可滚动) wpf c#
【发布时间】:2015-01-21 15:41:55
【问题描述】:

来自 SSH 客户端的输出被添加到 Results ObservableCollection 我得到了列表视图中显示的信息。

如何绑定可滚动的只读文本框以列出集合中的所有内容?

<!--<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>-->

如果有帮助的话,我已经有了一个皈依者

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

【问题讨论】:

  • 该转换器不适用于 ObservableCollection
  • 另外,可能想知道转换器在初始化时触发,但不是在添加新字符串时触发。
  • 那么一个有效的问题?目前尚不清楚您想做什么。所述输入与该转换器不兼容。
  • 有一个有效的问题,如果您忽略转换器。我只认为我需要一个转换器作为解决方案,所以我发布了一个。忽略那部分。
  • Hairydruidy 的答案有效,但你必须将它与stackoverflow.com/a/12346543/7821336 结合起来(对于TextBoxUtilities.AlwaysScrollToEnd)。

标签: c# wpf textbox observablecollection ivalueconverter


【解决方案1】:

转换器

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

文本框元素

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2020-05-09
    • 2018-09-03
    • 2011-05-07
    • 2017-05-31
    相关资源
    最近更新 更多