-1

My theory code:

ScriptContainerUserControl.xaml

<ItemsControl x:Name="ScriptItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
             <Grid>
             <TextBox x:Name="pTB" Text="{Binding PhasePriority}" />
             <TextBox x:Name="nTB" Text="{Binding Name}" />
             <TextBox x:Name="dTB" Text="{Binding Description}" />
             </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ScriptContainerUserControl.xaml.cs

public ScriptContainerUserControl() : base()
{
    InitializeComponent();
    ScriptItemsControl.ItemsSource = PScriptCollection;
}

//PScriptCollecion is of type SynchronizedObservableCollection<ProcessScript>
//ProcessScript has the elements PhasePriority, Name, and Description

Would the code above work for making sure

 ScriptItemsControl[i].dTB.Text = PScriptCollection[i].Description? 

Or is it not possible to bind like this?

Yael
  • 1,566
  • 3
  • 18
  • 25
K. Fenster
  • 91
  • 3
  • 15
  • You didn't even try it? A hint: Description must be a public property in the element class of PScriptCollection. – Clemens Jul 06 '16 at 20:36
  • I am trying to fix and issue that is preventing me from testing my theory. Each of the elements are public properties. – K. Fenster Jul 06 '16 at 20:41
  • Then it should work. Besides that, you can always write a small test program with exactly what you are showing here. Takes you five minutes. – Clemens Jul 06 '16 at 20:41
  • @Clemens in your opinion is it better practice to bind the ItemSource Property or does it really matter? – K. Fenster Jul 06 '16 at 20:53
  • It doesn't really matter in this case, because the source collection is a member of your UserControl and not a property of a view model. – Clemens Jul 06 '16 at 21:13

2 Answers2

1

Fenster,

It should definitely work, provided you have getter setter properties implemented for all the three properties in ProcessScript class.

When you use a datatemplate - it means you are setting the datacontext of each element of your itemscontrol to an element of your collection.

so here each Itemcontrol element will look at ProcessScript object and if that object has all three properties , you should see the data.

Vishal Harne
  • 191
  • 1
  • 9
0

It is not possible to do it in this way. You do not set Binding actually... To have support for observing a changes on collection you should bind the collection to ItemsSource property of ItemsControl. Instead of line:

ScriptItemsControl.ItemsSource = PScriptCollection;

try this

ScriptItemsControl.ItemsSource = new Binding("PScriptCollection");
bakala12
  • 368
  • 2
  • 15
  • Not true. Although it's common to bind the ItemsSource property, it isn't strictly necessary. You can as well make an ordinary assignment. – Clemens Jul 06 '16 at 20:36
  • Then my idea of trying to bind the items of the Collection to the items of the ItemsControl should work? – K. Fenster Jul 06 '16 at 20:37