为什么网格控制导致绑定失败?

时间:2017-02-19 09:58:29

标签: c# wpf data-binding

我有一个非常奇怪的问题,我似乎无法弄明白。如果我尝试绑定下面的第一个代码,网格内的绑定不起作用。

<Controls:Flyout IsOpen="{Binding Ui.EditGameFlyOut, Mode=TwoWay}" Header="{Binding SelectedGame.Id, StringFormat='Edit Game [{0}]'}" Position="Right" Theme="Dark" Width="300">
    <ScrollViewer>
        <Grid Margin="10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="200" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Button Width="100" Content="Save" Cursor="Hand" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,10" />
            <Label Grid.Row="1">Title</Label>
            <TextBox Grid.Row="2" Text="{Binding SelectedGame.Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Margin="0,0,0,10" />
            <Label Grid.Row="3">Release date</Label>
            <DatePicker Grid.Row="4" Margin="0,0,0,10" SelectedDate="{Binding DatePickerDate}" />
            <Label Grid.Row="5">Overview</Label>
            <TextBox Grid.Row="6" Text="" Grid.Column="1" Margin="0,0,0,10" />
        </Grid>
    </ScrollViewer>
</Controls:Flyout>

但是,如果我像第二个代码片段一样删除网格控件,它没有问题。为什么会这样?

<Controls:Flyout IsOpen="{Binding Ui.EditGameFlyOut, Mode=TwoWay}" Header="{Binding SelectedGame.Id, StringFormat='Edit Game [{0}]'}" Position="Right" Theme="Dark" Width="300">
    <ScrollViewer>
        <TextBox Grid.Row="2" Text="{Binding SelectedGame.Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Margin="0,0,0,10" />
    </ScrollViewer>
</Controls:Flyout>

我觉得这很奇怪......

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我设法解决了这个问题。

我必须做的是在网格上放置一个DataContext,例如下面的代码。

        <Controls:Flyout IsOpen="{Binding Ui.EditGameFlyOut, Mode=TwoWay}" Header="{Binding SelectedGame.Id, StringFormat='Edit Game [{0}]'}" Position="Right" Theme="Dark" Width="300">
            <ScrollViewer>
                <Grid Margin="10" DataContext="{Binding SelectedGame}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="200" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Button Width="100" Content="Save" Cursor="Hand" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,10" />
                    <Label Grid.Row="1">Title</Label>
                    <TextBox Grid.Row="2" Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,0,10" />
                    <Label Grid.Row="3">Release date</Label>
                    <DatePicker Grid.Row="4" Margin="0,0,0,10" SelectedDate="{Binding DatePickerDate}" />
                    <Label Grid.Row="5">Overview</Label>
                    <TextBox Grid.Row="6" Text="" Margin="0,0,0,10" />
                </Grid>
            </ScrollViewer>
        </Controls:Flyout>