0

I have a TextBox DataTemplate

    <DataTemplate x:Key="TextBoxTemplate">
        <StackPanel>
            <Label />
            <TextBox />
        </StackPanel>
    </DataTemplate>

And grid defined as

<Grid Margin="10 10 10 10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="25*"></ColumnDefinition>
        <ColumnDefinition Width="25*"></ColumnDefinition>
        <ColumnDefinition Width="25*"></ColumnDefinition>
        <ColumnDefinition Width="25*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ItemsControl ItemsSource="{Binding myObservableCollection}"  ItemTemplateSelector="{StaticResource myItemTemplateSelector}" />
</Grid>

What I'm trying to accomplish is to force this textbox to appear in particular grid's column - with no luck so far.

I tried

    <DataTemplate x:Key="TextBoxTemplate">
        <StackPanel>
            <Label />
            <TextBox Grid.Column="2" />
        </StackPanel>
    </DataTemplate>

but it doesn't work.

<DataTemplate x:Key="TextBoxTemplate">
    <StackPanel Grid.Column="2">
        <Label />
        <TextBox />
    </StackPanel>
</DataTemplate>

doesn't work either.

"2" is only for testing, ultimately I would like to use binding there.

Is it possible to do this with DataTemplate (to define in which grid's column I want TextBox to appear)?


I want to end up with TextBoxes placed like this.

enter image description here

So TextBox's position on grid should depend on value from template.

Modifying ItemsControl to appear in particular grid's column

<Grid Margin="10 10 10 10">
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="25*"></ColumnDefinition>
    <ColumnDefinition Width="25*"></ColumnDefinition>
    <ColumnDefinition Width="25*"></ColumnDefinition>
    <ColumnDefinition Width="25*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ItemsControl Grid.Column="2" ItemsSource="{Binding myObservableCollection}"  ItemTemplateSelector="{StaticResource myItemTemplateSelector}" />
</Grid>

is giving me this

enter image description here

which is not what I'm expecting. I want TextBoxes to be put in different columns, based on values from their templates.

a''
  • 2,322
  • 2
  • 23
  • 34
  • At the moment whole `ItemsControl`, with all items, takes **one cell** of your `Grid`. It's not clear to me what you're trying to achieve here. – dkozl Jun 18 '14 at 12:17
  • 1
    @erem Setting `Grid.Column` only effective if the element is direct child of a `Grid` – har07 Jun 18 '14 at 13:06
  • 1
    You can data bind to the `Grid.Column` and `Grid.Row` Attached Properties as you can see in the [Bind Grid.Row / Grid.Column inside a DataTemplate](http://stackoverflow.com/questions/2432847/bind-grid-row-grid-column-inside-a-datatemplate) question, but as @har07 correctly mentioned, it will only work if the element is in a `Grid`. – Sheridan Jun 18 '14 at 13:20

0 Answers0