0

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

我的要求是:

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

QuestionText、AnswerControl、AnswerDefaultText、IsItmandatory


Question1 TextBox null 是

QuestionText2,单选按钮,是的,是的

问题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;
}
}

}


}

我的转换器没有被调用,这段代码有什么问题吗?

4

1 回答 1

0

你需要使用这个:

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

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

您所做的是在您的资源中创建一个问题类并告诉 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 重命名为 Questions,以避免混淆。

于 2012-07-30T21:23:44.337 回答