0

I am using MVVMM Light WPF and I want to do the following: Generate textboxes dynamically and bind them to a property of a class.

I already have the following but it doesn't show up in my view when running the application.

This is my collection:

        private ObservableCollection<Border> _controllekes;
    public ObservableCollection<Border> Controllekes
    {
        get { return _controllekes; }
        set
        {
            _controllekes = value;
            RaisePropertyChanged("Controllekes");
        }
    }

This it my xaml:

                        <ItemsControl ItemsSource="{Binding Path=Controllekes}">

                    </ItemsControl>

This is a part where I fill the itemsource "Controllekes":

                Controllekes = new ObservableCollection<Border>();
            Border border = new Border();
            border.BorderThickness = new System.Windows.Thickness(5);
            border.BorderBrush = Brushes.AliceBlue;
            border.Padding = new System.Windows.Thickness(5);
            TextBlock tb = new TextBlock();
            tb.Background = Brushes.Red;
            Binding nameTextBinding = new Binding("Controllekes");
            nameTextBinding.Path = new System.Windows.PropertyPath(this.Dossier.Omschrijving);
            nameTextBinding.Mode = BindingMode.OneWay;
            //nameTextBinding.Source = this.Dossier.Omschrijving;
            tb.SetBinding(TextBlock.TextProperty, nameTextBinding);
            border.Child = tb;
            this.Controllekes.Add(border);

What it does it creates a border with in this border a textblock where the binding should happen. I whish to bind the property this.Dossier.Omschrijving (Dossier is the class). If I just enter a string in the textbox it works.

In runtime the border gets generated but the textblock remains empty. The object Dossier.Omschrijving contains information.

What do I do wrong?

EDIT:

safe put me in the right direction and the answer of ItemsControl with multiple DataTemplates for a viewmodel made me finish the job :)

Community
  • 1
  • 1
Kaizer
  • 651
  • 3
  • 9
  • 22

1 Answers1

0

through all that and use ItemTemplate

<ItemsControl x:Name="ic">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <CheckBox IsChecked="{Binding yourboolProperty}"/>
                <TextBlock Background="Red" Text="{Binding yourStringProperty}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

and set ic's ItemsSource to List

safi
  • 872
  • 6
  • 18
  • The thing is that the itemscontrol will be filled with textboxes, textblocks, checkboxes and radiobuttons. So I cannot do it like that – Kaizer Jan 16 '15 at 20:20
  • put all you want in ItemTemplate and bind only properties not controls – safi Jan 16 '15 at 20:48
  • I'm sorry..could you give me an example? – Kaizer Jan 16 '15 at 20:58
  • k..and how to fill the list with for example 2 textboxes created in code? – Kaizer Jan 16 '15 at 21:33
  • first i suppose to you reading more about ItemTemplate and DataTemplate in general. – safi Jan 16 '15 at 21:44
  • you can fill it by set list with two items, so the itemsControl fill its list with the number of item that you support, and it dynamically view each item with the shape of your ItemTemplate that you declare – safi Jan 16 '15 at 21:46
  • K thanks. I accept your answer cause I can see the solution thanks to your advice. – Kaizer Jan 17 '15 at 08:52
  • In addition I also managed to get it working thanks to this answer: http://stackoverflow.com/questions/5473001/itemscontrol-with-multiple-datatemplats-for-a-viewmodel – Kaizer Jan 17 '15 at 10:06