English 中文(简体)
如何将可观测的集合放入文本区块
原标题:How do you put an ObservableCollection into a Textblock

我要把我的 Obvilable Collection & lt; validationMessage & gt; 放到我的 TextBlock 。 这是我的代码。 它现在显示的是项目和子项目, 但是在信件显示有 system. colutions. ObjectModel. Objectable Collection 1[validationWP.Data sources.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

我认为这是因为它不能将 Ob可观集合 放在 TextBlock 中。

XAML: 时间轴:

<UserControl x:Class="ValidationWPF.ValidationUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:ValidationWPF.DataSources"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate x:Key="Messages">
            <TextBlock Text="{Binding Message}"/>
        </DataTemplate>



    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">

        <telerik:RadTreeView x:Name="radTreeView" Margin="8">
            <telerik:RadTreeView.ItemTemplate>



                <HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
                    <TextBlock Text="{Binding item}" />
                </HierarchicalDataTemplate>

            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>

    </Grid>
</UserControl>

验证邮件分类 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ValidationWPF.DataSources
{
    public class ValidationMessage
    {
        public ValidationMessage(string Message)
        {
            this.Message = Message;
        }

        public string Message
        {
            get;
            set;
        }

    }
}

校验项目类别 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace ValidationWPF.DataSources
{
   public class ValidationItem : ObservableCollection<ValidationItem>
    {
        public ValidationItem()
        {
            SubItems = new ObservableCollection<ValidationItem>();


        }


        public ObservableCollection<ValidationMessage> Message
        {
            get;
            set;
        }

        public string item
        {
            get;
            set;
        }

        public IList<ValidationItem> SubItems
        {
            get;
            set;
        }

        public static IList<ValidationItem> GetItems(string name)
        {
            var Validation = new ObservableCollection<ValidationItem>();


            var item = new ValidationItem();
            item.item = "Customer";


            var subItem = new ValidationItem();
            subItem.item = "Name";
            item.SubItems.Add(subItem);

            var Message = new ValidationItem();
            Message.item = new ObservableCollection<ValidationMessage>().ToString();
            subItem.SubItems.Add(Message);






            Validation.Add(item);

            return Validation;

        }
    }
}

谢谢你的帮助!

最佳回答

最后,我做了一个不同 和干净的方式。

XAML: 时间轴:

<UserControl x:Class="ValidationWPF.ValidationUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:ValidationWPF.DataSources"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <!--<local:CollectionConverter x:Key="CollectionConverter"/>
            <DataTemplate x:Key="Messages">
            <TextBlock Text="{Binding Message}"/>
        </DataTemplate>--> 




    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">

        <telerik:RadTreeView x:Name="radTreeView" Margin="8" ItemsSource="{Binding Errors}">
            <telerik:RadTreeView.ItemTemplate>
                 <HierarchicalDataTemplate ItemsSource="{Binding SubItems}" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Description}"/>

                        <ListBox Grid.Row="1" ItemsSource="{Binding Messages}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Message}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>

                </HierarchicalDataTemplate>

            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>

    </Grid>
</UserControl>

评估班:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ValidationWPF.DataSources
    {
        public class ValidationMessage
        {
            public ValidationMessage(string name, string Message)
            {
                this.Message = Message;
                this.PropertyName = name;
            }

            public string Message
            {
                get;
                set;
            }

            public string PropertyName { get; set; }

        }
    }

VALIDATIONVIEWMODEL CLASS:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;

    namespace ValidationWPF.DataSources
    {
        public class ValidationViewModel
        {
            public ValidationViewModel()
            {
                this.Errors = new ObservableCollection<ValidationItem>();

                ValidationItem item = new ValidationItem();
                item.Description = "Customer";

                ValidationMessage msg = new ValidationMessage("FirstName", "First name is required");
                item.Messages.Add(msg);


                this.Errors.Add(item);

                ValidationItem item2 = new ValidationItem();
                item2.Description = "Order";

                msg = new ValidationMessage("Quantity", "Quantity must be greater than zero");
                item2.Messages.Add(msg);


                item.SubItems.Add(item2);

            }

            public ObservableCollection<ValidationItem> Errors { get; set; }
        }
    }

估价控制分类:

  public partial class ValidationUserControl : UserControl
    {
        public ValidationUserControl()
        {
            InitializeComponent();
            this.DataContext = new ValidationViewModel();

        }

    }
问题回答

问题是, Text 属性的 TextBlock 是一个字符串,您又给它一个 Obvilable Collection 。 WPF唯一知道转换这两个字符串的方法是调用 Obvilable Collection. toString () , 返回该类的全部类型名称 。

修正是将您的 Obvisionable Collection 转换为字符串, 创建一个执行 < a href=> 的类 。 http:// msdn. microsoft. com/ en- us/library/ system. windows. data. ivalueconverter.aspx" rel=“ nofollow” > System. Windows.Data.IValue Convertier 。 这样您就可以控制转换 。

你可以执行它 类似这样的东西:

using System.Globalization;
using System.Text;
using System.Windows.Data;

namespace ValidationWPF.DataSources
{
    class CollectionConverter : IValueConverter
    {
        object Convert(object value, Type targetType,object parameter,CultureInfo culture)
        {
            ObservableCollection<ValidationMessage> messages = (ObservableCollection<ValidationMessage>)value;

            var sb = new StringBuilder();
            foreach(var msg in messages)
            {
                sb.AppendLine(msg.Message);
            }

            return sb.ToString();
        }

        object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

您可以在您的 XAML 文件中使用此功能 :

<UserControl x:Class="ValidationWPF.ValidationUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:ValidationWPF.DataSources"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <local:CollectionConverter x:Key="CollectionConverter" />

        <DataTemplate x:Key="Messages">
            <TextBlock Text="{Binding Message, Converter={StaticResource CollectionConverter}}"/>
        </DataTemplate>
    </UserControl.Resources>
    ...
</UserControl>

WPF 每当需要填充您的 TextBlock 时, 就会调用 CollectionConverter. convert ()

您是对的。 TextBlock 试图将属性值视为字符串和可观察的集合。 连接将返回您所看到的 。

您可以做的是添加一个新的属性, 将可观测集合的所有信息合并到一个字符串中。 类似 :

public string MessagesCombined  
{
   get { return string.Join(Environment.NewLine, Message.Select(m => m.Message)); } 
}

这将将您可观察的集合中的所有信件合并为单字符串, 每个项目都用新行分隔 。 ( 您可能需要修改我的代码, 我无法访问编译器... ) 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...