2017-02-19 59 views
0

我有一個非常奇怪的問題,我似乎無法弄清楚。如果我嘗試像下面的第一個代碼那樣綁定網格內部的綁定不起作用。爲什麼網格控件導致綁定失敗?

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

我覺得這很奇怪...

+0

什麼是輸出窗口說?它會顯示一個綁定問題,如果有的話。我看不到沒有任何理由讓網格破壞綁定 – nkoniishvt

+0

我的想法正確,如果我檢查輸出有安靜的幾個綁定警告,因爲我已經啓動之前綁定幾個模型。我應該解決這個問題,但是由於每一個其他的綁定工作,它不應該是問題。 '無法使用綁定檢索值並且不存在有效的回退值;如果你在「SelectedGame」中加入一個值,它會在輸出窗口中追加另一條消息嗎? –

+0

你也可以在你的SelectedGame和Title的getter中放置一個斷點,以知道你正在閱讀的是什麼值 – nkoniishvt

回答

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> 

相關問題