Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 4.0 在silverlight4中使用ContentControl和转换器动态创建控件_Silverlight 4.0 - Fatal编程技术网

Silverlight 4.0 在silverlight4中使用ContentControl和转换器动态创建控件

Silverlight 4.0 在silverlight4中使用ContentControl和转换器动态创建控件,silverlight-4.0,Silverlight 4.0,我在silverlight 4中创建动态控件时遇到问题 我的要求是: 我在数据库中有一个问题表,如下所示 QuestionText、AnswerControl、AnswerDefaultText、IsItmandatory 问题1文本框空是 问题文本2,单选按钮,是,是 问题3,组合框,空,否 我需要将这些数据放入对象中,并将问题文本转换为文本块,并基于answercontrol值,动态创建控件 正如您在文章中提到的,我尝试过,但数据没有绑定,无法将默认值作为参数值发送到转换器 我的转换器没

我在silverlight 4中创建动态控件时遇到问题

我的要求是:

我在数据库中有一个问题表,如下所示

QuestionText、AnswerControl、AnswerDefaultText、IsItmandatory


问题1文本框空是

问题文本2,单选按钮,是,是

问题3,组合框,空,否

我需要将这些数据放入对象中,并将问题文本转换为文本块,并基于answercontrol值,动态创建控件

正如您在文章中提到的,我尝试过,但数据没有绑定,无法将默认值作为参数值发送到转换器

我的转换器没有被调用。下面的代码有什么错误吗

我的代码是:

1) 我的Xaml代码:

<UserControl x:Class="SilverlightApplication5.DynamicControls"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication5.Converter"
xmlns:question="clr-namespace:SilverlightApplication5"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

   <Grid x:Name="LayoutRoot" Background="White" Width="400" Height="400">
      <Grid.Resources>
<local:UILocatorConverter x:Key="UILocatorConverter" />
<question:Questions x:Key="Questions"/>
</Grid.Resources>
<ListBox ItemsSource="{Binding Questions}" Width="400" Height="400">
<ListBox.ItemTemplate>
<DataTemplate>

<Grid> 
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>

<ContentControl Content="{Binding Converter={StaticResource UILocatorConverter},ConverterParameter={Binding QuestionText},Path=QuestionControl}" Grid.Column="0" />
<ContentControl Content="{Binding Converter={StaticResource UILocatorConverter},ConverterParameter={Binding DefaultValue},Path=AnswerControl}" Grid.Column="1" />

</Grid> 

</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
4) 我的问题是:

namespace SilverlightApplication5
{
public class Questions
{


private string _questionControl;
public string QuestionControl {
get
{
return _questionControl; 
}
set 
{
_questionControl = value;

}
}

private string _questionText;
public string QuestionText
{
get
{
return _questionText;
}
set
{
_questionText = value;

}
}

private string _answerControl;
public string AnswerControl
{
get
{
return _answerControl;
}
set
{
_answerControl = value;

}
}

private string _answerValues;
public string AnswerValues
{
get
{
return _answerValues;
}
set
{
_answerValues = value; 
}
}

private string _defaultValue;
public string DefaultValue
{
get
{
return _defaultValue;
}
set
{
_defaultValue = value;
}
}

}


}
我的转换器未被调用,此代码中是否存在任何问题?

您需要使用此代码:

<ListBox ItemsSource="{Binding}" Width="400" Height="400">
然后使用此绑定:

<ListBox ItemsSource="{Binding Question}" Width="400" Height="400"> 

我建议您将Questions类重命名为Question,然后将属性重命名为Questions,以避免混淆

<ListBox ItemsSource="{Binding}" Width="400" Height="400">
<question:Questions x:Key="Questions"/>
public partial class DynamicControls : UserControl
{
    public ObservableCollection<Questions> Question { get; set; }
    ...
DataContext = this;
<ListBox ItemsSource="{Binding Question}" Width="400" Height="400">