Dynamically Create controls using ContentControl and Converters
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">
</ ColumnDefinition>
</ ColumnDefinition>
I have a problem to create dynamic controls in silverlight 4.
This problem is similar to the solution provided in below thread.
Its working for static values, but its not working when i tried to use object.
My requiremnt is:
I have question table in database, which is like below.
QuestionText, AnswerControl, AnswerDefaultText, IsItmandatory
------------------------------ ------------------------------ -----------------
Question1 TextBox null Yes
QuestionText2, RadioButton, Yes, Yes
Question3, ComboBox, null, no
.............................. ............
I need to get this data into object and conver the question text into TextBlock, and based on answercontrol value, need to create controls dynamically.
I tried like as you mentioned in your post, but data is not bindind and not able to send default values as parameter values to conevrter.
My Codes are:
1)My Xaml Code:
xmlns:x="http://schemas.
xmlns:d="http://schemas.
xmlns:mc="http://schemas.
xmlns:local="clr-namespace:
xmlns:question="clr-namespace:
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
2) Code behind file code is:
namespace SilverlightApplication5
{
public partial class DynamicControls : UserControl
{
ObservableCollection< Questions> Question;
public DynamicControls()
{
InitializeComponent();
{
public partial class DynamicControls : UserControl
{
ObservableCollection<
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" });
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) My converter is:
namespace SilverlightApplication5. Converter
{
public class UILocatorConverter : IValueConverter
{
#region IValueConverter Members
{
public class UILocatorConverter : IValueConverter
{
#region IValueConverter Members
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() { };
{
String param="This is control created dynamically";
if (parameter != null)
{
param = System.Convert.ToString(
}
switch (value.ToString())
{
case "TextBlock":
return new TextBlock() { Text = param, HorizontalAlignment=
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();
}
{
throw new NotImplementedException();
}
#endregion
}
}
}
}
4) My Question Class is:
namespace SilverlightApplication5
{
public class Questions
{
{
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;
public string QuestionText
{
get
{
return _questionText;
}
set
{
_questionText = value;
}
}
}
private string _answerControl;
public string AnswerControl
{
get
{
return _answerControl;
}
set
{
_answerControl = value;
}
}
public string AnswerControl
{
get
{
return _answerControl;
}
set
{
_answerControl = value;
}
}
private string _answerValues;
public string AnswerValues
{
get
{
return _answerValues;
}
set
{
_answerValues = value;
}
}
public string AnswerValues
{
get
{
return _answerValues;
}
set
{
_answerValues = value;
}
}
private string _defaultValue;
public string DefaultValue
{
get
{
return _defaultValue;
}
set
{
_defaultValue = value;
}
}
public string DefaultValue
{
get
{
return _defaultValue;
}
set
{
_defaultValue = value;
}
}
}
}
My converter is not getting called, is ther any issues in this code.
Can you please help me.
Thanks In advance
Sukesh
Answers & Comments...
Answer: 1Hi,
Please refer to the code below:
" xmlns:x="http://schemas.Answer: 1Hi,
Please refer to the code below:
http://schemas. microsoft.com/winfx/2006/xaml/ presentation
namespace SilverlightApplication5.Converter { public class UILocatorConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization. CultureInfo culture) { Questions question = value as Questions; String param = "This is control created dynamically"; String ControlType = "TextBlock"; if (parameter.Equals("Question")) { param = question.QuestionText; ControlType = question.QuestionControl; } else { param = question.AnswerValues; ControlType = question.AnswerControl; } switch (ControlType.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(); } #endregion } }
You can find I changed a lot:
1.
Bind to the Questions directly, not Questions.Questions
2.
Bind to the whole object. Then deal with the data in the converter code-behind
by :
Answer: 2Thank you very much Otomii Lu!!!.
It worked as my expection, Thank you for your time in changing the code.
Thanks
Sukesh
by :
Answer: 3
I am trying to get the valuesprovided in answer control back in objects.
For that I have put Mode=TwoWay, it had asked me to provide the path value. I am bit confused on what to give in Path, since content property is already getting control object.
Still i tried to provide below values but no use.
Path=Questions -> Converter not getting called.
Path=AnswerText -> only answer text is sending into converter, not full object
Path=. -> Not working
Path=DataContext -> Full collection is sending into converter,but not current object
Can you please help me.
Thanks
Sukeshby :
Answer: 4I am able to do it throug converter itself. Here is my modified converter code, its working fine.
case "TextBox":{TextBox txtAnswer = new TextBox() { Name = question.AnswerControlID, Width = 150};Binding bind = newBinding() { Path = new PropertyPath("AnswerText"), Mode = BindingMode.TwoWay };txtAnswer.SetBinding(TextBox
by :
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog