Why i can't found the memeber AlternationCount in a listview XAML in my Windows8 application??
Best regards
To do this you could use the ItemContainerGenerator property and chain its ContainerFromItem and IndexFromContainer methods to get the index of your item and then use a converter to get a background color from the index.
public class Item : BindableBase
{
public ItemsControl itemsControl {get;set;}
private string name;
public string Name
{
get{
return name;
}
set
{
name = value;
OnPropertyChanged();
}
}
public int Index
{
get
{
return itemsControl.ItemContainerGenerator.IndexFromContainer(
itemsControl.ItemContainerGenerator.ContainerFromItem(this)
);
}
}
}
public sealed partial class ItemContainerGeneratorTest : App1.Common.LayoutAwarePage
{
...
public ObservableCollection<Item> test
{
get
{
var test = new ObservableCollection<Item>();
test.Add(new Item() { Name = "Index for item 1: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 2: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 3: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 4: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 5: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 6: ", itemsControl = ItemsControlControl });
return test;
}
}
...
}
<ItemsControl x:Name="ItemsControlControl" ItemsSource="{Binding ElementName=pageRoot, Path=test}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Index}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Note that this example doesn't currently handle changes to the collection (i.e. OnPropertyChanged isn't called for Index when the collection changes).
Related
I have a Weather VM, that get my city and country code perfectly, but I am trying to bind the rest of my data to XAML.
All my data is store in the array, and for some reason, when I loop, the variable i, does not increment and the app crash.
I also have the problem of the icon, I have a list, that is the itemsource of my Listview, but the Icon is on another array
I tried to loop, and assign each variable, and the loop always crash
var splitedData = cityData.Split(",");
var city = splitedData[0];
var conutryCode = splitedData[1];
var data = await WeatherAPI.GetWeatherDataAsync(city, conutryCode);
if (data != null) {
for (int i = 0; i < data.list.Count; i++) {
rootObject.list[i].dt_txt = data.list[i].dt_txt;
rootObject.list[i].main.temp_max = data.list[i].main.temp_max;
rootObject.list[i].main.temp_min = data.list[i].main.temp_min;
rootObject.list[i].weather[i].icon
}
}
}
xaml listView
Margin="20" ItemsSource="{Binding rootObject.list}">
<ListView.ItemTemplate>
<DataTemplate>
<RelativePanel>
<TextBlock x:Name="dateTB"
Text="{Binding dt_txt}"
RelativePanel.RightOf="iconTB"
RelativePanel.AlignTopWith="iconTB" />
<TextBlock x:Name="highTB"
Text="{Binding main.temp_max}"
RelativePanel.RightOf="iconTB"
RelativePanel.Below="dateTB"
FontSize="10" />
<TextBlock x:Name="lowTB"
RelativePanel.RightOf="highTB"
RelativePanel.Below="dateTB"
FontSize="10"
Text="{Binding main.temp_min}"
Margin="10,0,0,0" />
<Image x:Name="iconTB"
Source="{}"
Height="30"
Width="30"
/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I expect the Dates to appear, the High to appear and the low to appear.
Also I have don't have a clue of how to bind the icon, becouse the icon is in Weather, and not the RootObject list, that is the itemsource of my list
I tried your code in my side and find something might be the reason. The problem might be that you didn't create instances for main object or weather list or day list. So you are assigning values to null and then it gives error.
I changed it a little bit, you can try in your side.(I still can't use MapLocator.GetCityData(); so I haven't tried this with real data.)
public class RootObject : Notify
{
private List<Day> _list;
public List<Day> list
{
get { return _list; }
set
{
if (value != _list)
{
_list = value;
onPropertyChanged("list");
}
}
}
public RootObject()
{
//I added five objects for test. In real scenario, the count depends on the data number you get
list = new List<Day>();
list.Add(new Day());
list.Add(new Day());
list.Add(new Day());
list.Add(new Day());
list.Add(new Day());
}
}
public class Day : Notify
{
private Main _main;
public Main main
{
get { return _main; }
set
{
if (value != _main)
{
_main = value;
onPropertyChanged("main");
}
}
}
private List<Weather> _weather;
public List<Weather> weather
{
get { return _weather; }
set
{
if (value != _weather)
{
_weather = value;
onPropertyChanged("weather");
}
}
}
private string _dt_txt;
public string dt_txt
{
get { return _dt_txt; }
set
{
if (value != _dt_txt)
{
_dt_txt = value;
onPropertyChanged("dt_txt");
}
}
}
public Day()
{
main = new Main();
//I added five objects for test. In real scenario, the count depends on the data number you get
weather = new List<Weather>();
weather.Add(new Weather());
weather.Add(new Weather());
weather.Add(new Weather());
weather.Add(new Weather());
weather.Add(new Weather());
}
}
For local test, I just added 5 objects. In your real scenario, you need to check the count of data from bingmap.
I want to show a label when i click on my item in my listview.
The real problem i don't know how to link between my viewmodel and my views
I want modify my label in viewmodel but I don't know if its possible currently.
My xaml :
<StackLayout>
<Label x:Name="labelperso"
Text="{Binding newProduct}"
IsVisible="{Binding Addproduct}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
BackgroundColor="#000000"
FontSize="20"
Opacity="0"/>
<ListView ItemsSource="{Binding Products}" CachingStrategy="RecycleElement" RowHeight="50" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding CodeReferenceLibelle}" TextColor="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemSelected" Command="{Binding
SelectCommand}" Converter="{StaticResource SelectedItemConverter}"/>
</ListView.Behaviors>
my viewmodel :
#region labelperso property
private string _newProduct;
public string newProduct
{
get { return _newProduct; }
set { SetProperty(ref _newProduct, value); }
}
#endregion
#region Addproduct property
private bool _Addproduct;
public bool Addproduct
{
get { return _Addproduct; }
set { SetProperty(ref _Addproduct, value); }
}
#endregion
when I click on my item :
async Task Select()
{
newProduct = "Produit ajouté !";
basketManager.AddProductSkuAsync(sku);
newProduct = "";
await Task.Run(() => ShowText());
}
//I have tried this but I can't use my label in my view
async Task ShowText()
{
await labelperso.FadeTo(1);
await Task.Delay(1000);
await labelperso.FadeTo(0);
}
Why are you want to take the label "labelperso" in VM ? you can use it in xaml.cs instead.
You just need to add the event ItemSelected like this:
<ListView ItemsSource="{Binding Products}" ItemSelected="OnSelection">
In xaml.cs
void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
//suppose the binding Object is Product
Product product = (Product)e.SelectedItem;
//labelperso.Text = "name = " + product.Name;
labelperso.FadeTo(1);
Task.Delay(1000);
labelperso.FadeTo(0);
}
Normally, VM are unrelated to Xaml, and we should not get labels from VM.
And we don't recommend it.But if you must, you can pass the Label in from the xaml.cs file like this:
You can define a variable in yourpage.xaml.cs:
public Label pageLabel;
and initial like this:
pageLabel = labelperso;
BindingContext = new YourViewmodel(this);
And in YourViewmodel.cs:
public Label ss;
public YourViewmodel(ContentPage parentPage)
{// here HomePage is your contentPage name of the page`
ss = ((HomePage)parentPage).pageLabel;//after this you can use it
}
You need to add a SelectedProduct property to your VM.
private string _SelectedProduct;
public string SelectedProduct
{
get { return _SelectedProduct; }
set { SetProperty(ref _SelectedProduct, value); }
}
You can then bind your ListView's SelectedItem to it
<ListView ItemsSource="{Binding Products}"
SelectedItem="{Binding SelectedProduct}"
CachingStrategy="RecycleElement"
RowHeight="50" >
You can then control the visibility of your label by binding to SelectedProduct via a "nullToVisibility" converter, or by using triggers etc.
You should try to use MVVM pattern rather than hacking with code behind.
Using MVVM you can add a Visible property to your viewmodel and bind the IsVisible property of the label to it.
Code will be much easy to read and maintain.
HI i try to reorder items in ItemsControl . when i set ManipulationMode of element in ItemsControl to ManipulationModes.TranslateY ScrollView dosent work. for this problem i change this ManipulationMode in Holding event but i faced a new problem.
in my code,we need release finger after holding and touch again for moving. i need holding and moving finger to drag items not holding and release and touch again and moving. this release is to bad for me and users
please help me.
my code:
<ItemsControl ItemsSource="{Binding Items}"
x:Name="todoList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="{Binding Path=color, Converter={StaticResource ColorToBrushConverter}}"
Height="75"
x:Name="todoItem">
<i:Interaction.Behaviors>
<Behaviors:DragReOrderBehavior />
</i:Interaction.Behaviors>
<Grid Background="{StaticResource itemGradient}">
<!--task text-->
<TextBlock Text="{Binding Title}"
Margin="15,15,0,15" FontSize="25" TextWrapping="Wrap"
x:Name="taskText"/>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
and behavior:
public class DragReOrderBehavior : DependencyObject, IBehavior
{
private static readonly int AutoScrollHitRegionSize = 80;
private DispatcherTimer _autoScrollTimer;
private FrameworkElement _dragItem;
private int _initialDragIndex;
private bool IsActive = false;
private ItemsControl itemscontrolList;
private ItemViewModel draggedItemVM;
private MainViewModel VM;
private ResettableObservableCollection<ItemViewModel> itemsListVm;
private ScrollViewer _scrollViewer;
// private SoundEffect _moveSound;
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
var fw = (FrameworkElement)AssociatedObject;
//fw.ManipulationMode = ManipulationModes.TranslateY | ManipulationModes.System;
////fw.ManipulationStarted += fw_ManipulationStarted;
fw.Holding += fw_Holding;
//fw.ManipulationDelta += fw_ManipulationDelta;
//fw.ManipulationCompleted += fw_ManipulationCompleted;
if (fw.RenderTransform == null || fw.RenderTransform as TranslateTransform == null)
{
fw.RenderTransform = new TranslateTransform();
}
// a timer which is used to periodically detect the position of the
// item being dragged in order to allow auto-scroll behaviour
_autoScrollTimer = new DispatcherTimer();
_autoScrollTimer.Interval = TimeSpan.FromMilliseconds(50);
_autoScrollTimer.Tick += (s, e) =>
{
AutoScrollList();
ShuffleItemsOnDrag();
};
// _moveSound = SoundEffect.FromStream(TitleContainer.OpenStream("Sounds/Windows XP Menu Command.wav"));
}
void fw_Holding(object sender, HoldingRoutedEventArgs e)
{
//if (Math.Abs(e.Cumulative.Translation.Y) > Math.Abs(e.Cumulative.Translation.X))
// {
//var fw = (FrameworkElement)AssociatedObject;
//fw.ManipulationMode = ManipulationModes.TranslateRailsY | ManipulationModes.System;
IsActive = true;
// locate the element being dragged
_dragItem = AssociatedObject as FrameworkElement;
itemscontrolList = AssociatedObject.Ancestors<ItemsControl>().OfType<ItemsControl>().FirstOrDefault();
draggedItemVM = ((ItemViewModel)_dragItem.DataContext);
VM = (MainViewModel)itemscontrolList.DataContext;
itemsListVm = VM.Items;
_scrollViewer = itemscontrolList.Descendants<ScrollViewer>()
.Cast<ScrollViewer>()
.SingleOrDefault();
_dragItem.SetVerticalOffset(0);
_dragItem.ManipulationMode = ManipulationModes.TranslateY;
_dragItem.ManipulationDelta += fw_ManipulationDelta;
_dragItem.ManipulationCompleted += fw_ManipulationCompleted;
_dragItem.UpdateLayout();
itemscontrolList.UpdateLayout();
_initialDragIndex = itemsListVm.IndexOf(draggedItemVM);
// fade out the items in the list, other than the dragged one
foreach (var item in itemscontrolList.GetItemsInView()
.Where(i => i.DataContext != draggedItemVM))
{
item.Animate(1.0, 0.7, "Opacity", 300, 0);
}
_autoScrollTimer.Start();
// }
}
}
The ListView & GridView support reordering items. How about thinking of using them?
First you can disable this feature by setting CanDragItems CanReorderItems AllowDrop to false and when you hold the item in your ListView then setting the properties above to true which will enable the Reorder feature.
For now, I have something like that (2 columns with dropboxes containing values independent from each other):
<xcdg:DataGridControl.Columns>
<xcdg:Column Title="A"
FieldName="A"
CellContentTemplate="{StaticResource ADT}"
GroupValueTemplate="{StaticResource ADT}"
Converter="{StaticResource AConverter}"
CellEditor="{StaticResource AEditor}"/>
<xcdg:Column Title="B"
FieldName="B"
CellContentTemplate="{StaticResource BDT}"
GroupValueTemplate="{StaticResource BDT}"
Converter="{StaticResource BConverter}"
CellEditor="{StaticResource BEditor}"/>
</xcdg:DataGridControl.Columns>
And I would like the B column to be a dropbox containing values depending on the value selected in the first column.
I don't know how to achieve that. I read about Binding.RelativeSource but I think it is not at all what I should use.
Thanks
I can think of two ways to do that, and since you didn't provide your exact case, i will provide a simple scenario and build my answer base on it,
let say you have a DataGrid with two editable columns (A and B), in the edit mode you can choose the A value from a combobox list, and then the B combobox will be filtered to show only the items whom their value starts with the A value for example, if A="aa", B should be {"aaaa","aabb"}, to implement that you need first a Model that represent the DataGrid Items
public class GridItem
{
public String A { get; set; }
public String B { get; set; }
}
In your codebehind / ViewModel define those properties (the DataGrid , and the comboboxes ItemSource Collections) :
private ObservableCollection<GridItem> _gridItemsCollection = new ObservableCollection<GridItem>()
{
new GridItem()
{
A="aa",
B="bbbb"
}
};
public ObservableCollection<GridItem> GridItemsCollection
{
get
{
return _gridItemsCollection;
}
set
{
if (_gridItemsCollection == value)
{
return;
}
_gridItemsCollection = value;
OnPropertyChanged();
}
}
//for the first Combobox
private ObservableCollection<String> _aCollection = new ObservableCollection<String>()
{
"aa",
"bb"
};
public ObservableCollection<String> ACollection
{
get
{
return _aCollection;
}
set
{
if (_aCollection == value)
{
return;
}
_aCollection = value;
OnPropertyChanged();
}
}
//for the second Combobox
private ObservableCollection<String> _bCollection ;
public ObservableCollection<String> BCollection
{
get
{
return _bCollection;
}
set
{
if (_bCollection == value)
{
return;
}
_bCollection = value;
OnPropertyChanged();
}
}
Define a full B collection from which your B combobox's itemsource will be populated
ObservableCollection<String> MainBCollection = new ObservableCollection<String>()
{
"aaaa",
"aabb",
"bbaa",
"bbbb"
};
Finally the population of the B combobox will be based on the selected item in the A combobox using this property,
private String _selectedAItem;
public String SelectedAItem
{
get
{
return _selectedAItem;
}
set
{
if (_selectedAItem == value)
{
return;
}
_selectedAItem = value;
OnPropertyChanged();
var returnedCollection = new ObservableCollection<String>();
foreach (var val in MainBCollection)
{
if (val.StartsWith(_selectedAItem))
{
returnedCollection.Add(value);
}
}
BCollection = new ObservableCollection<string>(returnedCollection);
}
}
You need of course to implement the INotifypropertyChanged Interface, so that the B Combobox Itemsource will be updated,
Now regarding the Xaml, due to some limitations in Xceed you need to specify the Combobox's ItemSource and SelectedItem using the RelativeSource and Ancestor binding,
<Grid >
<xcdg:DataGridControl ItemsSource="{Binding GridItemsCollection}" AutoCreateColumns="False" SelectionMode="Single" >
<xcdg:DataGridControl.Columns>
<xcdg:Column Title="A"
FieldName="A"
>
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
<xcdg:Column.CellEditor>
<xcdg:CellEditor>
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox Name="AComboBox" SelectedItem="{Binding SelectedAItem, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}" SelectedValue="{xcdg:CellEditorBinding}"
ItemsSource="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type wpfApplication3:MainWindow}},
Path=ACollection}">
</ComboBox>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
</xcdg:Column.CellEditor>
</xcdg:Column>
<xcdg:Column Title="B"
FieldName="B"
>
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
<xcdg:Column.CellEditor>
<xcdg:CellEditor>
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox Name="AComboBox" SelectedValue="{xcdg:CellEditorBinding}" ItemsSource="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type Window}},
Path=BCollection}">
</ComboBox>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
</xcdg:Column.CellEditor>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
and the result is something like that
The Other way to do that is by using a MultivalueConverter and update the B Collection eachtime the A SelectedValue is changed,
something like that :
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox Name="AComboBox" SelectedValue="{xcdg:CellEditorBinding}">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource BCollectionConverter}">
<Binding Path="BCollection" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
<Binding Path="SelectedValue" ElementName="AComboBox" />
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
And implement the converter to update the B Combobox's ItemSource,
public class BCollectionConverter:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null)
return null;
var bCollection = (values[0] as ObservableCollection<String>);
var aSelectedItem = (values[1] as String);
if (aSelectedItem == null)
return null;
var returnedCollection = new ObservableCollection<String>();
foreach (var value in bCollection)
{
if (value.StartsWith(aSelectedItem))
{
returnedCollection.Add(value);
}
}
return returnedCollection;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I didn't try that last one, you might as well give it a try, I hope that did help.
I have a problem to create dynamic controls in silverlight 4.
My requirement 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 binding and not able to send default values as parameter values to converter.
My Converter is not getting called. Is there any thing wrong in below code?
My Codes are:
1)My Xaml Code:
<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) Code behind file code is:
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) My converter is:
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) My Question Class is:
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;
}
}
}
}
My converter is not getting called, is there any issues in this code?
You need to use this:
<ListBox ItemsSource="{Binding}" Width="400" Height="400">
As you want to access the collection of Questions you set in the DataContext.
What you were doing was creating a single Questions class in your Resources and telling the ListBox to use that.
So you won't need this at all:
<question:Questions x:Key="Questions"/>
(You might have to use BindsDirectlyToSource...because you are setting the DataContext to a collection directly...could be wrong on that!).
Alternatively, you can do this in your control:
public partial class DynamicControls : UserControl
{
public ObservableCollection<Questions> Question { get; set; }
...
Set the DataContext in this way:
DataContext = this;
And then use this Binding:
<ListBox ItemsSource="{Binding Question}" Width="400" Height="400">
I'd recommend you rename your Questions class to Question, and then rename the Property to Questions, to avoid confusion.