jubair
jubair

Reputation: 597

how to bind a grid with an object

I am an amateur to windows phone development and also new to wpf. I have a grid :

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >

                        <Grid HorizontalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>



                            <TextBlock Text="{Binding StrDay}" 
                                        Grid.Row="0"
                                        Margin="9,-7,0,0" 
                                        Style="{StaticResource PhoneTextTitle1Style}"/>

                            <TextBlock Text="Day's Highlight" 
                                        Grid.Row="1"
                                        FontWeight="Bold"
                                        Style="{StaticResource PhoneTextNormalStyle}"
                                        />

                            <TextBlock Text="{Binding DaysHighlight}"
                                        Grid.Row="2"
                                        Style="{StaticResource PhoneTextNormalStyle}"
                                        />

                            <TextBlock Text="My Whole Day"
                                        Grid.Row="3"
                                        FontWeight="Bold"
                                       Style="{StaticResource PhoneTextNormalStyle}"
                                        />

                            <TextBlock Text="{Binding WholeDay}"
                                        Grid.Row="4"
                                        Style="{StaticResource PhoneTextNormalStyle}"
                                        />
                        </Grid>

I want to bind it with the DayDetail object. I should mention that DayDetail is not a collection. it is just an object of a class that has StrDay, DaysHighlight, WholeDay Property. I am following MVVM structure.

public void loadSelectedData(int Id)
        {
            try
            {
                DayDetail = myDiaryData.tblMyDailyDiary.Single(details => details.Id == Id);

            }

            catch (Exception e)
            {
            }

        }

Upvotes: 1

Views: 1677

Answers (1)

jubair
jubair

Reputation: 597

I've found the solution.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{Binding DayDetail}" >

just adding DataContext="{Binding DayDetail}" bind the whole thing. thanks for everyone's help. it is working nice for me

Upvotes: 1

Related Questions