使用新的 DataContext WPF 刷新网格
我有一个网格,其中域服务绑定到数据上下文。我想刷新该数据上下文。
我有代码:
<Grid DataContext="{Binding Data, ElementName=userDomainDataSource}" Name="gridAccountDetails">
<sdk:Label Content="Address Line" HorizontalAlignment="Left" Margin="443,67,0,0" d:LayoutOverrides="VerticalAlignment" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="537,59,0,0" x:Name="txtAccountDetailsAddress" Text="{Binding addressline, Mode=OneWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="VerticalAlignment" Height="24" VerticalAlignment="Top" DataContext="{Binding}" />
<sdk:Label Content="County" HorizontalAlignment="Left" Margin="443,142,0,143" d:LayoutOverrides="Height" />
<TextBox HorizontalAlignment="Left" Margin="537,134,0,143" x:Name="txtAccountDetailsCounty" Text="{Binding county, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" />
<sdk:Label Content="Email" HorizontalAlignment="Left" Margin="26,143,0,142" d:LayoutOverrides="HorizontalAlignment, Height" />
<TextBox HorizontalAlignment="Left" Margin="114,136,0,142" x:Name="txtAccountDetailsEmail" Text="{Binding email, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="First Name" HorizontalAlignment="Left" Margin="26,67,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="114,60,0,0" x:Name="txtAccountDetailsFirstname" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="Last Name" HorizontalAlignment="Left" Margin="26,104,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="114,97,0,0" x:Name="txtAccountDetailsLastname" Text="{Binding lastname, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="Postcode" HorizontalAlignment="Left" Margin="443,0,0,104" VerticalAlignment="Bottom" />
<TextBox HorizontalAlignment="Left" Margin="537,0,0,104" x:Name="txtAccountDetailsPostcode" Text="{Binding postcode, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
<sdk:Label Content="Town" Margin="0,105,323,0" VerticalAlignment="Top" HorizontalAlignment="Right" />
<TextBox HorizontalAlignment="Left" Margin="537,0,0,180" x:Name="txtAccountDetailsTown" Text="{Binding town, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
<Button Content="Edit Account Details" HorizontalAlignment="Right" Margin="0,0,26,35" VerticalAlignment="Bottom" Width="136"/>
</Grid>
我已经使用代码设置了域源:
EntityQuery<Web.User> qry =
from u in usrClass.getQuery()
where u.userId == usrClass.getUserId()
select u;
LoadOperation<Web.User> loadU = usrClass.loadUsersQuery(qry);
userDomainDataSource.DataContext = loadU.Entities;
我尝试使用:
gridAccountDetails.DataContext = userDomainDataSource.DataContext;
但
gridAccountDetails.DataContext = userDomainDataSource.Data;
没有运气
但我想刷新数据源,以便将新值放入文本框中,因为用户可以更改这些值。
任何人都可以提供任何信息。谢谢
I have a grid with a domain service binded to the data context. I would like to refresh that data context.
I have the code:
<Grid DataContext="{Binding Data, ElementName=userDomainDataSource}" Name="gridAccountDetails">
<sdk:Label Content="Address Line" HorizontalAlignment="Left" Margin="443,67,0,0" d:LayoutOverrides="VerticalAlignment" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="537,59,0,0" x:Name="txtAccountDetailsAddress" Text="{Binding addressline, Mode=OneWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="VerticalAlignment" Height="24" VerticalAlignment="Top" DataContext="{Binding}" />
<sdk:Label Content="County" HorizontalAlignment="Left" Margin="443,142,0,143" d:LayoutOverrides="Height" />
<TextBox HorizontalAlignment="Left" Margin="537,134,0,143" x:Name="txtAccountDetailsCounty" Text="{Binding county, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" />
<sdk:Label Content="Email" HorizontalAlignment="Left" Margin="26,143,0,142" d:LayoutOverrides="HorizontalAlignment, Height" />
<TextBox HorizontalAlignment="Left" Margin="114,136,0,142" x:Name="txtAccountDetailsEmail" Text="{Binding email, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="First Name" HorizontalAlignment="Left" Margin="26,67,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="114,60,0,0" x:Name="txtAccountDetailsFirstname" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="Last Name" HorizontalAlignment="Left" Margin="26,104,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="114,97,0,0" x:Name="txtAccountDetailsLastname" Text="{Binding lastname, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />
<sdk:Label Content="Postcode" HorizontalAlignment="Left" Margin="443,0,0,104" VerticalAlignment="Bottom" />
<TextBox HorizontalAlignment="Left" Margin="537,0,0,104" x:Name="txtAccountDetailsPostcode" Text="{Binding postcode, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
<sdk:Label Content="Town" Margin="0,105,323,0" VerticalAlignment="Top" HorizontalAlignment="Right" />
<TextBox HorizontalAlignment="Left" Margin="537,0,0,180" x:Name="txtAccountDetailsTown" Text="{Binding town, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
<Button Content="Edit Account Details" HorizontalAlignment="Right" Margin="0,0,26,35" VerticalAlignment="Bottom" Width="136"/>
</Grid>
I have set the domain source with the code:
EntityQuery<Web.User> qry =
from u in usrClass.getQuery()
where u.userId == usrClass.getUserId()
select u;
LoadOperation<Web.User> loadU = usrClass.loadUsersQuery(qry);
userDomainDataSource.DataContext = loadU.Entities;
I have tried using:
gridAccountDetails.DataContext = userDomainDataSource.DataContext;
and
gridAccountDetails.DataContext = userDomainDataSource.Data;
with no luck
But I would like to refresh the datasource so the new values get put into the textboxes, as these can be changed by the user.
Can anyone provide any information. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么你必须告诉“gridAccountDetails”“userDomainDataSource”的“数据”发生了变化。最广泛使用的方法是引发 PropertyChanged 事件,尽管我不确定这如何适合您的服务,可能会考虑从服务到网格的缓冲区。
还有一件事一旦绑定,我就不会碰你的网格的数据上下文,你要绑定它然后覆盖。
Well you have to tell "gridAccountDetails" that the "Data" of "userDomainDataSource" changed. Most widely used for this is to raise a PropertyChanged event altough I'm not sure how this would fit with your service, possibly consider a buffer from the service to your Grid.
One more thing once binded I wouldn't touch the datacontext of your Grid, you're binding it and then overwriting.