2013-06-05 89 views
0

我有觀察/問題/問題。WPF未將值設置爲設置爲Datacontext的EF對象

在我的應用程序中,我已經將我設置爲「DataContext」的EF對象(我們稱之爲「Car」)列表加載到「ItemsControl」中。 ItemsControl有一堆代表對象(「汽車」)的屬性,如文本框和類似的控件...

據我瞭解,如果我將一個屬性分配給控件,它應該綁定如果我更改了TextBox中的值,它也應該在對象中更改。但是這不會發生在我的應用程序中。我錯過了什麼嗎?

據我所知,相反,當屬性的類需要實現接口「INotifyPropertyChanged」,當Object-> GUI發生變化時,但是當這種變化發生變化時,就不需要這樣做來自GUI對象。

我錯過了什麼,我是否想知道,或者只是寫錯誤的代碼?

編輯 - 第1次 - 日Thnx的建議

<ItemsControl Name="icRoles" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" > 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid Name="grdRoles" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
       <Grid.RowDefinitions> 
        <RowDefinition Height="80" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="40" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="1.4*"/> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <!--Subject--> 

       <Label Content="Uloga" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Center" Name="label8" VerticalAlignment="Center" /> 

       <Label Content="ID:" Grid.Row="1" Height="28" Name="label1" VerticalAlignment="Top" /> 
       <Label Content="Ime Uloge:" Grid.Row="2" Height="28" Name="label2" VerticalAlignment="Top" /> 
       <Label Content="Prikazano ime:" Grid.Row="3" Height="28" Name="label3" VerticalAlignment="Top" /> 
       <Label Content="Tip Uloge:" Grid.Row="4" Height="28" Name="label4" VerticalAlignment="Top" /> 
       <Label Content="Datum Stvaranja:" Grid.Row="5" Height="28" Name="label5" VerticalAlignment="Top" /> 
       <Label Content="Datum promjene:" Grid.Row="6" Height="28" Name="label6" VerticalAlignment="Top" /> 
       <Label Content="Stanje:" Grid.Row="7" Height="28" Name="label7" VerticalAlignment="Top" /> 

       <TextBox Grid.Column="1" Grid.Row="1" Height="23" Text="{Binding ID}" IsEnabled="False" /> 
       <TextBox Grid.Column="1" Grid.Row="2" Height="23" Text="{Binding Name}" /> 
       <TextBox Grid.Column="1" Grid.Row="3" Height="23" Text="{Binding Text}" /> 
       <WrapPanel Grid.Column="1" Grid.Row="4" Height="25" /> 
       <DatePicker Grid.Column="1" Grid.Row="5" Height="25" SelectedDate="{Binding DateCreated}" IsEnabled="False" /> 
       <DatePicker Grid.Column="1" Grid.Row="6" Height="25" SelectedDate="{Binding DateModified}" IsEnabled="False" /> 
       <CheckBox Grid.Column="1" Grid.Row="7" Height="16" IsChecked="{Binding active}" /> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

編輯 - 第2次 我現在已經設置綁定控件的方式,因爲它們supossed是。但一件奇怪的事情正在發生。我已經設置了兩個文本框的綁定爲

Text="{Binding Path=Name, Mode=TwoWay}" 
Text="{Binding Path=Text, Mode=TwoWay}" 

但是隻有第一個被設置爲propery。第一個留空。+

+0

發佈至少綁定Xaml。 –

+1

你可以發佈你的代碼嗎?再次檢查「DataContext」並將綁定模式設置爲兩種方式 – Jasti

+0

認爲我發現它 http://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode.aspx – user853710

回答

0

好的。從建議解決它從User that helped

我錯過了綁定模式。 (Text =「{Binding Path = Name,Mode = TwoWay}」)

第二個問題是因爲控件沒有將數據保存到對象中,因爲它仍然有焦點。保存沒有被提交到對象

相關問題