misleadingTitle
misleadingTitle

Reputation: 647

Databind to the datacontext element of the row

I've this itemtemplate binded on a ObservableCollection<MembershipUserProfiled> element so it can be displayed in a ListBox.

<UserControl.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="ListItemElencoTemplate">

            <Grid Background="#FFFFFF" Width="300">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="110" />
                    <ColumnDefinition Width="110" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=FirstName}" Margin="0 0 0 3" />
                <Label Grid.Column="1" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=LastName}" Margin="0 0 0 3" />
                <Label Grid.Column="2" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=BaseUser.UserName}" Margin="0 0 0 3" />
                <Button Content="Button" Style="{DynamicResource edit}" Click="Edit_Click" CommandParameter="{Binding Path=...}" HorizontalAlignment="Right" Grid.Column="4" Width="20" Height="20"/>
            </Grid>

        </DataTemplate>
    </ResourceDictionary>
</UserControl.Resources>

Everything works just fine but I want to bind the button CommandParameter to the MembershipUserProfiled object of the row, in order to pass the data on the click action.

I've tried different solution but nothing seems to work. What I'm trying to do is feasible?

Upvotes: 0

Views: 44

Answers (1)

Mohd Ahmed
Mohd Ahmed

Reputation: 1482

You can try CommandParameter="{Binding}" this will automatically bind the MembershipUserProfiled object

<Button Content="Button" CommandParameter="{Binding}" Style="{DynamicResource edit}" Click="Edit_Click"  HorizontalAlignment="Right" Grid.Column="4" Width="20" Height="20"/>

Upvotes: 1

Related Questions