首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ContentControl和silverlight4中的转换器动态创建控件

使用ContentControl和silverlight4中的转换器动态创建控件
EN

Stack Overflow用户
提问于 2012-07-29 08:23:47
回答 1查看 699关注 0票数 0

在silverlight 4中创建动态控件有一个问题。

我的要求是:

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

QuestionText,AnswerControl,AnswerDefaultText,IsItmandatory

Question1 TextBox null是

QuestionText2,RadioButton,是,是

Question3,ComboBox,null,no

.

我需要将这些数据输入到对象中,并将问题文本转换到TextBlock中,并根据应答控件的值动态创建控件。

我尝试过像您在文章中提到的那样,但是数据不是绑定的,并且无法将默认值作为参数值发送到转换器。

我的转换器不会被呼叫。下面的代码有什么问题吗?

我的密码是:

1)我的Xaml代码:

代码语言:javascript
复制
<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)文件代码后面的代码是:

代码语言:javascript
复制
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)我的转换器是:

代码语言:javascript
复制
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)我的问题课是:

代码语言:javascript
复制
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;
}
}

}


}

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

EN

回答 1

Stack Overflow用户

发布于 2012-07-30 21:23:44

你需要用这个:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding}" Width="400" Height="400">

因为您想访问您在DataContext中设置的问题集合。

您所做的是在参考资料中创建一个问题类,并告诉ListBox使用这个类。

所以你根本不需要这个

代码语言:javascript
复制
<question:Questions x:Key="Questions"/>

(您可能必须使用BindsDirectlyToSource...because,您正在将DataContext设置为集合directly...could!)。

或者,您可以在您的控制中这样做:

代码语言:javascript
复制
public partial class DynamicControls : UserControl
{
    public ObservableCollection<Questions> Question { get; set; }
    ...

以这种方式设置DataContext:

代码语言:javascript
复制
DataContext = this;

然后使用这个绑定:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Question}" Width="400" Height="400"> 

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11707799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档