我有观察/问题/问题。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。第一个留空。+
发布至少绑定Xaml。 –
你可以发布你的代码吗?再次检查“DataContext”并将绑定模式设置为两种方式 – Jasti
认为我发现它 http://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode.aspx – user853710