使用silverlight4中的ContentControl和Converters动态创建控件



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

我的要求是:

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

QuestionText,AnswerControl,AnswerdDefaultText,是否强制


问题1文本框为空是

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

问题3,组合框,空,无

我需要将这些数据获取到对象中,并将问题文本转换到TextBlock中,并且基于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>

2) 文件代码背后的代码是:

namespace SilverlightApplication5
{
public partial class DynamicControls : UserControl
{
ObservableCollection<Questions> Question;
public DynamicControls()
{
InitializeComponent();
Question = new ObservableCollection<Questions>();
Question.Add(new Questions { QuestionControl = "TextBlock", QuestionText = "What is your name?", AnswerControl = "TextBox", AnswerValues = "", DefaultValue = "" });
Question.Add(new Questions { QuestionControl = "TextBlock", QuestionText = "What is your surname?", AnswerControl = "TextBox", AnswerValues = "", DefaultValue = "" });
Question.Add(new Questions { QuestionControl = "TextBlock", QuestionText = "Sex:", AnswerControl = "ComboBox", AnswerValues = "Male,Female,Others", DefaultValue = "Select a Value" });
Question.Add(new Questions { QuestionControl = "TextBlock", QuestionText = "Marital Status", AnswerControl = "RadioButton", AnswerValues = "", DefaultValue = "Not Married" });
this.DataContext = Question;
}
}
}

3) 我的转换器是:

namespace SilverlightApplication5.Converter
{
public class UILocatorConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String param="This is control created dynamically";
if (parameter != null)
{
param = System.Convert.ToString(parameter);
}
switch (value.ToString())
{
case "TextBlock":
return new TextBlock() { Text = param, HorizontalAlignment=HorizontalAlignment.Center,TextWrapping=TextWrapping.NoWrap,Width=200 };
case "Button":
return new Button() { Content = param, Width=150 };
case "TextBox":
return new TextBox() { Text = param };
case "RadioButton":
return new TextBox() { };
case "ComboBox":
return new TextBox() { };

}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

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

当您想要访问在DataContext中设置的问题集合时。

您所做的是在资源中创建一个Questions类,并告诉ListBox使用它。

所以你根本不需要这个:

<question:Questions x:Key="Questions"/>

(您可能必须使用BindsDirectlyToSource…,因为您直接将DataContext设置为集合…这可能是错误的!)。

或者,你可以在自己的控制下这样做:

public partial class DynamicControls : UserControl
{
    public ObservableCollection<Questions> Question { get; set; }
    ...

以这种方式设置DataContext:

DataContext = this;

然后使用此绑定:

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

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

最新更新