Search code examples
c#wpfxamldata-bindingitemscontrol

Binding Property of an Item in an ItemsControl From a Collection


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?


Solution

  • 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.