让 ItemsSource 将集合视为单个项目,而不是对其进行迭代?

发布于 2025-01-01 08:06:49 字数 559 浏览 5 评论 0原文

我有一个显示对象属性的TreeView。属性之一是大字节数组。

TreeView 的 ItemsSource 显然将其视为子节点的集合,但我真正想要的是一个子节点,显示仍然可以展开/折叠的整个数组。

例如,目前

- MyObject
    Prop1
    Prop2
  - PropWithBytes
      1
      2
      3
      etc

我想要的:

- MyObject
    Prop1
    Prop2
  - PropWithBytes
      1, 2, 3  etc

有什么方法可以告诉树将集合视为单个节点吗?

如果我不希望实际数据仍然是一个单独的节点,我可以轻松编写一个 DataTemplate(而不是 HierarchicalDataTemplate)来就地显示它。我还可以使用使用 Expander 的 DataTemplate 来隐藏数据,该数据工作正常,但看起来很难看。

I have a TreeView showing an object's properties. One of the properties is a large byte array.

TreeView's ItemsSource obviously treats this as a collection of child nodes, but what I actually want is ONE child node showing the entire array that can still be expanded/collapsed.

eg currently

- MyObject
    Prop1
    Prop2
  - PropWithBytes
      1
      2
      3
      etc

What I want:

- MyObject
    Prop1
    Prop2
  - PropWithBytes
      1, 2, 3  etc

Is there any way to tell the Tree to treat the collection as a single node?

If I didn't want the actual data to still be a separate node I could easily write a DataTemplate (as opposed HierarchicalDataTemplate) to to display it in place. I can also use a DataTemplate that uses an Expander instead to hide the data which works fine, but looks ugly.

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

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

发布评论

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

评论(1

音栖息无 2025-01-08 08:06:49

如果我正确理解你的问题 - 尝试使用 ValueConverter 它将检查传递的值是否为字节数组(如果是字节数组 - 返回它的字符串表示形式)。

public class ByteArrayValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value is IEnumerable<byte>)
            return string.Join(", ", (IEnumerable<byte>)value);
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

If I understand your question correctly - try to use ValueConverter which will check if passed value is byte array or not (in case of byte array - returns it's string representation).

public class ByteArrayValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value is IEnumerable<byte>)
            return string.Join(", ", (IEnumerable<byte>)value);
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文