如何将网格与对象绑定

时间:2013-07-16 18:43:16

标签: c# wpf windows-phone-8

我是Windows手机开发的业余爱好者,也是wpf的新手。我有一个网格:

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

我想将它与DayDetail对象绑定。我应该提到DayDetail不是一个集合。它只是一个具有StrDay,DaysHighlight,WholeDay Property的类的对象。我正在关注MVVM结构。

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

            }

            catch (Exception e)
            {
            }

        }

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

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

只需添加DataContext="{Binding DayDetail}"即可绑定整个内容。谢谢大家的帮助。它对我很好