如何在 WPF 数据绑定中使用的文本框上设置多行属性

发布于 2024-11-10 01:22:18 字数 289 浏览 7 评论 0原文

我是数据绑定的新手,但运行了一个简单的程序,将一串数据绑定到文本框。这很有效,每次更新字符串时它都会显示在文本框中。

但是,我需要我的文本框之一附加数据,而不是用新的数据字符串覆盖它。 1.我知道我可以将新的数据字符串附加到现有的数据字符串中,但是从长远来看,这不会影响性能,因为这会导致每次更新时整个字符串都与文本框绑定? 2.您对如何实现附加而不必担心每次更新时将整个字符串复制到文本框有什么建议吗?

该字符串用于向屏幕输出一些日志信息(通过文本框控件),它注定会变得非常大。

希望大家能够帮忙,或者给点建议。

I am new to databinding, but got a simple program running where I bind a string of data to a textbox. This works well, everytime the string is updated it shows it in the textbox.

However I have a need for one of my textboxes to append the data rather than to overwrite it with the new data string.
1. I know that I can append the new data string to the existing one, but would that not impact on performance in the long run, as this will cause the entire string to be bounded with the textbox every time an update is made?
2. Do you perhaps have any suggestions on how I can achieve the appending without having to worry that the entire string is copied to the textbox every time it is updated?

This string is used to output some logging information to the screen (via textbox control) and it is destined to become very large.

Hope that you can help, or give some suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-11-17 01:22:19

没有一种方法可以使用 WPF 数据绑定来附加一些数据,并且您的观察是正确的,即每次更新日志字符串时绑定都会重新评估。如果字符串变得很长,这可能会损害性能。我可以建议不同的设计吗?与其将日志存储为单个字符串,为什么不将其存储为离散日志消息列表呢?然后,您可以将其绑定到 UI 中的列表。当添加新的日志项目时,它只会在列表中添加一个新项目。此设计还可以利用 UI 虚拟化。

一个快速代码示例:

ObservableCollection<string> logItems = new ObservableCollection<string>();

public ObservableCollection<string> LogItems
{
  get { return _logItems; }
}

// add items as they are logged
public AddToLog(string message)
{
  logItems.Add(message);
}

然后您可以将此日志消息集合绑定到您的 UI:

<ListBox x:Name="list" ItemsSource="{Binding LogItems}"/>

或者如果您想在代码隐藏中绑定...

list.SetBinding(ListBox.ItemsSource, new Binding("LogItems")
{
  Source = this;
});

There isn't a way that you can use a WPF databinding to append some data, and you are correct in your observation that each time your log string is updated the binding will re-evaluate. This could harm performance if the string gets very long. Can I suggest a different design? Instead of storing your log as a single string, why not store it as a list of discrete log messages? You can then bind this to a list in your UI. When new log items are added, it will simply add a new item within the list. This design can also take advantage of UI virtualization.

A quick code example:

ObservableCollection<string> logItems = new ObservableCollection<string>();

public ObservableCollection<string> LogItems
{
  get { return _logItems; }
}

// add items as they are logged
public AddToLog(string message)
{
  logItems.Add(message);
}

You can then bind this collection of log messages to your UI:

<ListBox x:Name="list" ItemsSource="{Binding LogItems}"/>

Or if you want to bind in code-behind ...

list.SetBinding(ListBox.ItemsSource, new Binding("LogItems")
{
  Source = this;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文