渲染使用的IValueConverter仅使用C#code的UI [英] Rendering a UI using IValueConverter using just c# code

查看:84
本文介绍了渲染使用的IValueConverter仅使用C#code的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和 Silverlight的5 初学者。

首先,我不得不用反序列化XML字符串创建对象。我这样做是成功地,但现在我的下一个步骤是使用对象元素来创建GUI。我有想法,我必须使用的IValueConverter 这样做的。但如何,我不知道。

First i had to create object by deserializing an xml string. I have done that succesfully but now my next step is to create GUI using the object elements. I have idea that i have to use "IValueConverter" for doing this. But how that i dont know.

包含对象我的计划类是这样的:

My Program class which contains the object is like this:

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

编辑:
我有它是如何实现的小想法:

I have little idea of how it can be achieved:

的第一件事是在含有的IValueConverter接口我们有(使用转换()函数)中的对象(在反序列化获得)转换到的参数,然后通过传递(含有在这里c#中创建的组合框)那些parameteres类收益到XAML codewhich包含容器来渲染我们只是用C#创建的GUI。

First thing is in the class containing "IValueConverter" interface we have to convert(using Convert() function) the objects(obtained on deserializing) in to parameters and then pass those parameteres (containing combo box created in c# here) through return to the xaml codewhich contains the container to render the GUI we just created using c#.

和在XAML code,我们只需要创建容器,将显示previous步组合框和其他标签和文字,我们在c#code创建。 (我们没有在这里使用XAML创建组合框,它是在C#code创建的类包含的IValueConverter界面,返回UI内)。

And in Xaml code we just need to create container which will display the combo box and other labels and text we created in c# code in previous step. (We don't have to create combo box using xaml here, it is created in c# code inside the class containing IValueConverter interface which returns the UI).

例如:(这是粗略的想法,使你明白不当,可能会有一些错误syntatical):

For example: (It's rough idea to make you understand properly, there may be some syntatical error):

我的MyValueConverter.cs级是假设:

My "MyValueConverter.cs" class is suppose:

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接口类)创建的UI。 所以我们要选择哪些必须能够渲染快照的GUI(conatiner可能是StackPanel的,因为有要显示一个以上的东西)<所示的组合框,标签,文本中的所有DATAS中/强任何容器>。

Whereas in xaml file i have to create a container that will render the UI created in c#(IValueConverter interface class). So we have to chose any container which must be capable of rendering the combo box , label, text all the datas shown in GUI of snapshot (conatiner could be StackPanel because there are more than one thing to be displayed).

我的XAML code将只是containign这样的容器:

my xaml code will be just containign a container like this:

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

在achieveing​​它,请任何帮助吗?请不要犹豫,问,如果弄不明白呢。

Any help in achieveing it please ? Please do not hesitate to ask if couldn't understand yet.

推荐答案

您可以使用隐式数据类型这一点。

You could use Implicit Data Types for this.

在你的XAML可以定义一个特定的数据类型的模板:

In your xaml you define a template for a certain datatype:

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

您更好地创造取决于类型元素值不同的类型。另一种解决方案是创建该类型参数的大模板,并显示相应的内容取决于什么参数类型包含的内容。但我不会推荐这种方法。

You better create different types depending on the type-element value. Another solution is to create a large template for the type Parameter, and show the appropriate elements depending on what the Parameter-type contains. But I wouldn't recommend this approach.

然后你可以使用一个ItemsControl显示所有参数:

Then you can use an ItemsControl to display all parameters:

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

的不同参数将在取决于它是什么类型的不同的方式来呈现。

The different parameters will be rendered in different ways depending on what type it has.

这篇关于渲染使用的IValueConverter仅使用C#code的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆