0

Silverlight 4 で動的コントロールを作成する際に問題があります。

私の要件は次のとおりです。

以下のようなデータベースに質問表があります。

QuestionText、AnswerControl、AnswerDefaultText、IsItmandatory


Question1 TextBox null はい

QuestionText2、RadioButton、はい、はい

Question3、ComboBox、null、いいえ

...................................................

このデータをオブジェクトに取得し、質問テキストを 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 で設定した質問のコレクションにアクセスしたい場合。

あなたが行っていたのは、Resources に単一の 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 の名前を Questions に変更することをお勧めします。

于 2012-07-30T21:23:44.337 に答える