以编程方式设置组件和列表中的值之间的绑定

| 我正在Silverlight 3中开发应用程序,并且有一个动态表单,我想从属性列表(键值)生成此表单,我该如何设置组件之间的绑定(复选框) ,TextBox,...)和属性值? 该代码仅是解决方案的第一近似值,而不是确定的代码:
 int numeroFila = 0;
 MainPage rootPage = ((App)Application.Current).RootVisual as MainPage;
        rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Clear();

        foreach (var atributo in ListaAtributos)
        {
            string tipoAtributo = ObtenerDefinicionAtributo(atributo.Key);
            FrameworkElement campoDatos;
            TextBlock bloqueTexto = new TextBlock();
            bloqueTexto.Text = atributo.Key;
            bloqueTexto.Margin = new Thickness(10,3,0,0);

            switch (tipoAtributo)
            {
                case \"Boolean\":

                    CheckBox campoBooleano = new CheckBox();
                    campoBooleano.Name = atributo.Key;
                    campoBooleano.IsChecked = ObtenerValorCampoBooleano(atributo.Value);
                    campoDatos = campoBooleano;
                    break;

                case \"DateTime\":

                    DatePicker campoFecha = new DatePicker();
                    try
                    {
                        campoFecha.DisplayDate = DateTime.Parse(atributo.Value);
                    }
                    catch (Exception) 
                    {
                        campoFecha.DisplayDate = DateTime.Now;
                    }
                    campoDatos = campoFecha;
                    break;

                default:

                    TextBox campoTexto = new TextBox();
                    campoTexto.Text = atributo.Value == null ? \"\" : atributo.Value;
                    campoDatos = campoTexto;
                    break;
            }

            campoDatos.Margin = new Thickness(0,1,10,1);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.RowDefinitions.Add(new RowDefinition());
            Grid.SetColumn(campoDatos, 1);
            Grid.SetColumn(bloqueTexto, 0);
            Grid.SetRow(campoDatos, numeroFila);
            Grid.SetRow(bloqueTexto, numeroFila);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Add(bloqueTexto);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Add(campoDatos);
            numeroFila++;
        }

    }
    
已邀请:
        我找到了解决方案。这是代码:
        int numeroFila = 0;
        MainPage rootPage = ((App)Application.Current).RootVisual as MainPage;
        rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Clear();

        foreach (var atributo in ListaAtributos)
        {
            string tipoAtributo = ObtenerDefinicionAtributo(atributo.Key);
            FrameworkElement campoDatos;
            TextBlock bloqueTexto = new TextBlock();
            bloqueTexto.Margin = new Thickness(10,3,0,0);
            Binding bind = new Binding();
            bind.Source = atributo;
            bind.Path = new PropertyPath(\"Key\");
            bind.Mode = System.Windows.Data.BindingMode.TwoWay;
            bloqueTexto.SetBinding(TextBlock.TextProperty, bind);


            switch (tipoAtributo)
            {
                case \"Boolean\":

                    CheckBox campoBooleano = new CheckBox();
                    campoBooleano.Name = atributo.Key;
                    campoBooleano.IsChecked = ObtenerValorCampoBooleano(atributo.Value);
                    bind = new Binding();
                    bind.Source = atributo;
                    bind.Path = new PropertyPath(\"Value\");
                    bind.Mode = System.Windows.Data.BindingMode.TwoWay;
                    campoBooleano.SetBinding(CheckBox.IsCheckedProperty, bind);
                    campoDatos = campoBooleano;
                    break;

                case \"DateTime\":

                    DatePicker campoFecha = new DatePicker();
                    try
                    {
                        campoFecha.DisplayDate = DateTime.Parse(atributo.Value);
                    }
                    catch (Exception) 
                    {
                        campoFecha.DisplayDate = DateTime.Now;
                    }
                    bind = new Binding();
                    bind.Source = atributo;
                    bind.Path = new PropertyPath(\"Value\");
                    bind.Mode = System.Windows.Data.BindingMode.TwoWay;
                    campoFecha.SetBinding(DatePicker.TextProperty, bind);
                    campoDatos = campoFecha;
                    break;

                default:

                    TextBox campoTexto = new TextBox();
                    atributo.Value = atributo.Value == null ? \"\" : atributo.Value;
                    bind = new Binding();
                    bind.Source = atributo;
                    bind.Path = new PropertyPath(\"Value\");
                    bind.Mode = System.Windows.Data.BindingMode.TwoWay;
                    campoTexto.SetBinding(TextBox.TextProperty, bind);
                    campoDatos = campoTexto;
                    break;
            }

            //this.GetType().GetProperty(\"\").GetGetMethod().Invoke(new Object(), null);

            campoDatos.Margin = new Thickness(0,1,10,1);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.RowDefinitions.Add(new RowDefinition());
            Grid.SetColumn(campoDatos, 1);
            Grid.SetColumn(bloqueTexto, 0);
            Grid.SetRow(campoDatos, numeroFila);
            Grid.SetRow(bloqueTexto, numeroFila);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Add(bloqueTexto);
            rootPage.NuevoElementoWindowInstance.NuevoElementoInstance.ListadoAtributos.Children.Add(campoDatos);
            numeroFila++;
        }
但是现在,我需要将Converter设置为CheckBox,因为al \“ Values \”是字符串,并且我需要转换string-boolean 提前致谢。     

要回复问题请先登录注册