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



我有一个非常奇怪的问题,我似乎无法弄清楚。如果我尝试像在网格内部的绑定下面的第一个代码一样绑定,则无效。

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

我觉得这真的很奇怪……

我在大量研究后设法修复了它。

我要做的就是将数据台面放在网格上,例如下面的代码。

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

最新更新