WPF没有将值设置为设置为数据上下文的EF对象
本文关键字:设置 上下文 EF 对象 数据 WPF | 更新日期: 2023-09-27 18:16:58
我有一个观察/问题/问题。
在我的应用程序中,我加载了一个EF对象列表(让我们称之为"Car"),我将其设置为"DataContext"到"ItemsControl"。ItemsControl有一堆控件,代表对象("Car")的属性,如文本框和类似....
据我所知,如果我给一个控件分配了一个属性,它应该被绑定,如果我改变了文本框中的值,它也应该在对象中改变。但我的应用没有发生这种情况,我错过了什么吗?
据我所知,对于另一种方式,属性的类需要实现接口"INotifyPropertyChanged"时发生的变化从对象->GUI,但没有这样的事情需要做时,从GUI-Object的变化。
我错过了什么,是我被误导了,还是只是写了错误的代码?
第一次编辑 -谢谢你的建议
<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>
编辑-第二次我现在已经将绑定模式设置为控件,因为它们应该是。但一件奇怪的事情正在发生。我已经将两个文本框的Binding设置为
Text="{Binding Path=Name, Mode=TwoWay}"
Text="{Binding Path=Text, Mode=TwoWay}"
但是只有第一个被设置为属性。+
OK。从帮助用户的建议中解决
我错过了绑定模式。(Text="{Binding Path=Name, Mode= two - way}")
第二个问题是因为控件没有将数据保存到对象中,因为它仍然有焦点。保存未提交到对象