c# - 仅使用 C# 代码使用 IValueConverter 呈现 UI

标签 c# silverlight data-binding binding

我是 c# 和 silverlight-5 初学者。

首先,我必须通过反序列化 xml 字符串来创建对象。我已经成功地做到了这一点,但现在我的下一步是使用对象元素创建 GUI。我知道我必须使用“IValueConverter”来执行此操作。但我不知道这是怎么回事。

我的包含对象的程序类是这样的:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Collections;
namespace Model.XML
{
    public class ProgramControl
    {
        public static void Main()
        {
            string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> 
                       <parameter>  
                       <name>max_amount</name>
                       <label>Max Amount</label>
                       <unit>Millions</unit>
                       <component>
                       <type>Combo</type>
                       <attributes>
                       <type>Integer</type>
                       <displayed>4</displayed>
                       <selected>0</selected>
                       <items>
                       <item>5</item>
                       <item>10</item>
                       <item>20</item>
                       <item>50</item>
                       </items>
                       </attributes>
                       </component >
                       </parameter>";    

            XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));
            XmlReader reader = XmlReader.Create(new StringReader(xmlstring));

            Parameter parameter = (Parameter)deserializer.Deserialize(reader);


            foreach (var item in parameter.Component.Attributes.Items)
            {
                Debug.WriteLine(item);
            }    

            Debug.WriteLine(parameter.Component.Type);
            Debug.WriteLine(parameter.Name);
            Debug.WriteLine(parameter.Label);
            Debug.WriteLine(parameter.Unit);

        } 
    }
}

现在的问题是如何从反序列化 (IValueConverter) 获得的对象创建 GUI?

编辑: 我不知道如何实现它:

首先是在包含“IValueConverter”接口(interface)的类中,我们必须将对象(在反序列化时获得)转换(使用 Convert() 函数)到参数中,然后将这些参数(包含在 c# 中创建的组合框)传递给返回 到包含容器的 xaml 代码,以呈现我们刚刚使用 c# 创建的 GUI。

在 Xaml 代码中,我们只需要创建一个容器来显示我们在上一步中用 C# 代码创建的组合框和其他标签和文本。 (我们不必在这里使用 xaml 创建组合框,它是在包含返回 UI 的 IValueConverter 接口(interface)的类内的 c# 代码中创建的)。

例如:(粗略的让大家理解正确,可能会有一些语法错误):

假设我的“MyValueConverter.cs”类:

public class MyValueConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture) 
    {

        List<parameter> list = value as List<Parameter>;
        List<UIElement> result = new List<UIElement>();

        foreach(parameter p in list)
        {
            UIElement newEle = null;
            if (p.component.type == "Combo")
            {
                newEle = new ComboBox();

            }
            result.add(newEle);
        }
        return result;
        /////////////////////////////////////////////////
        //////////////// and so on ://///////////////////
        /////////////////////////////////////////////////
    }
}

而在 xaml 文件中,我必须创建一个容器来呈现在 c#(IValueConverter 接口(interface)类)中创建的 UI。 所以我们必须选择任何容器,它必须能够渲染组合框、标签、文本以及快照 GUI 中显示的所有数据(容器可以是 StackPanel,因为要显示的东西不止一个).

我的 xaml 代码将只包含一个像这样的容器:

<UserControl x:Class="RenderingTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:this="clr-namespace:RenderingTest.Converters"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <this:MyValueConverter x:Key="ImageConverter"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" Width="Auto" Height="Auto">
        <!-- There should be container here to render the combo box
             created using c# code in "MyValueConverter" class -->
    </Grid>
</UserControl>

在实现它方面有什么帮助吗?如果还不明白,请随时询问。

最佳答案

您可以为此使用隐式数据类型。

在您的 xaml 中,您为特定数据类型定义了一个模板:

<DataTemplate DataType="ComboParameter">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text={Binding Path=label}" />
            <ComboBox ItemsSource="{Binding Path=items}"/>
            <TextBlock Text="{Binding Path=unit}"/>
        </StackPanel>
</DataTemplate>

您最好根据类型元素值创建不同的类型。另一种解决方案是为 Parameter 类型创建一个大模板,并根据 Parameter-type 包含的内容显示适当的元素。但我不推荐这种方法。

然后您可以使用 ItemsControl 来显示所有参数:

<ItemsControl ItemsSource="{Binding Path=Parameters}" />

不同的参数将根据它的类型以不同的方式呈现。

关于c# - 仅使用 C# 代码使用 IValueConverter 呈现 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23488983/

相关文章:

c# - 如何从 View 模型访问不可绑定(bind)的 View 属性?

c# - 允许使用 DataGridView 双向绑定(bind)到 IQueryable 的好方法是什么?

javascript - AngularJS - 删除绑定(bind)以避免内存泄漏

c# - 在任务计划程序中配置 exe 时,检索具有 CLSID {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败

c# - Visual Studio 2008 编辑器配置 - 插入符和当前行

silverlight - 防止许多未关闭的Silverlight套接字连接出现堆栈溢出

Silverlight TextBlock 文本属性与内容

c# - 从后台线程通知 UI 线程

c# - 为什么我在创建 TIFF 时在我的服务器上收到异常?

c# - 如何在子类中创建动态类型并分配它?