我在使用Silverlight数据网格中的组合框时遇到问题,两者都绑定到DataContext,后者又使用RIA从数据库中检索数据。
我遇到的问题是当您在组合框中选择一个值并单击关闭单元格而不是显示新选择的值的单元格时,单元格显示为空。但是 - 只有在数据网格的另一个单元格中选择尚未选择的值时才会出现这种情况!选择唯一值会导致该框显示为空
我的代码如下:
<navigation:Page x:Class="Chemical_Management_System.EditPermissions"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="EditPermissions Page" Width="640" HorizontalAlignment="Center"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:my="clr-namespace:Chemical_Management_System.Web"
xmlns:myn="clr-namespace:Chemical_Management_System"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot">
<Border BorderBrush="Silver" BorderThickness="1" Height="45" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Border1" Style="{StaticResource HeaderBackground}" VerticalAlignment="Top" Width="618">
<TextBlock Height="23" Name="TextBlock21" Style="{StaticResource HeaderText}" Text="Edit Permissions" />
</Border>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Permissions, CreateList=true}" Height="0" Name="PermissionsDomainDataSource" QueryName="GetPermissionsQuery" SubmittedChanges="OnSubmitChanges" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my:DataContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:PermissionLookups, CreateList=true}" Height="0" Name="PermissionLookupsDomainDataSource" QueryName="GetPermissionLookupsQuery" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my:DataContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Users, CreateList=true}" Height="0" Name="UsersDomainDataSource" QueryName="GetUsersQuery" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my:DataContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<Button Content="Apply" Height="23" HorizontalAlignment="Left" Margin="553,445,0,0" Name="btnApply" VerticalAlignment="Top" Width="75" />
<sdk:DataGrid AutoGenerateColumns="False" Height="378" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=PermissionsDomainDataSource, Path=Data}" Margin="12,61,0,0" Name="PermissionsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="615">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="User" MinWidth="190">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding User.Name}" Padding="5,0,0,0" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding UsersDomainDataSource}"
Loaded="ComboBoxUsers_Loaded"
SelectionChanged="UsersComboBox_SelectionChanged"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Permission" MinWidth="175">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding PermissionLookup.Description}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PermissionLookupsDomainDataSource}"
Loaded="ComboBox_Loaded"
SelectionChanged="PermissionLookupComboBox_SelectionChanged"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="DescriptionColumn" Binding="{Binding Path=Description}" Header="Description" MinWidth="245" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Content="New" Height="23" HorizontalAlignment="Left" Margin="12,445,0,0" Name="btnNew" VerticalAlignment="Top" Width="75" />
<Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="93,445,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="472,445,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" />
</Grid>
Partial Public Class EditPermissions
Inherits Page
Dim Context As New Web.DataContext()
Dim user As New CurrentUser()
Public Sub New()
InitializeComponent()
AddHandler user.userLoadedCompleted, AddressOf userLoaded
End Sub
'Executes when the user navigates to this page.
Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
End Sub
Private Sub userLoaded()
If user.permissionCheck("Admin") = False Then
MessageBox.Show("You do not have permission to access this page")
NavigationService.Navigate(New Uri("/Home", UriKind.Relative))
End If
End Sub
Private Sub PermissionsDomainDataSource_LoadedData(ByVal sender As System.Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles PermissionsDomainDataSource.LoadedData
If e.HasError Then
System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
e.MarkErrorAsHandled()
End If
End Sub
Private Sub PermissionLookupsDomainDataSource_LoadedData(ByVal sender As System.Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles PermissionLookupsDomainDataSource.LoadedData
If e.HasError Then
System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
e.MarkErrorAsHandled()
End If
End Sub
Private Sub ComboBox_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
' set combobox items source from wherever you want
Dim combo As ComboBox = sender
combo.ItemsSource = Me.PermissionLookupsDomainDataSource.DataView
End Sub
Private Sub ComboBoxUsers_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
' set combobox items source from wherever you want
Dim combo As ComboBox = sender
combo.ItemsSource = Me.UsersDomainDataSource.DataView
End Sub
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnApply.Click
Me.PermissionsDomainDataSource.SubmitChanges()
NavigationService.Navigate(New Uri("/AdminHome", UriKind.Relative))
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
If PermissionsDataGrid.SelectedItem IsNot Nothing Then
Dim selected As Web.Permissions = PermissionsDataGrid.SelectedItem
Me.PermissionsDomainDataSource.DataView.Remove(selected)
Else
MessageBox.Show("Please select a permission entry to delete")
End If
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNew.Click
Dim permission As New Web.Permissions
Me.PermissionsDomainDataSource.DataView.Add(permission)
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
Me.PermissionsDomainDataSource.RejectChanges()
NavigationService.Navigate(New Uri("/AdminHome", UriKind.Relative))
End Sub
Private Sub OnSubmitChanges(ByVal sender As Object, ByVal e As SubmittedChangesEventArgs)
If (e.HasError) Then
MessageBox.Show("Error: " + e.Error.ToString)
End If
End Sub
Private Sub UsersComboBox_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
Dim permission As Web.Permissions = PermissionsDataGrid.SelectedItem
Dim selected As ComboBox = sender
Dim user As Web.Users = selected.SelectedItem
permission.UserID = user.ID
selected.UpdateLayout()
End Sub
Private Sub PermissionLookupComboBox_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
Dim permission As Web.Permissions = PermissionsDataGrid.SelectedItem
Dim selected As ComboBox = sender
Dim permissionLookup As Web.PermissionLookups = selected.SelectedItem
permission.PermissionID = permissionLookup.ID
End Sub
End Class
答案 0 :(得分:0)
这是可能的解决方案之一..
答案 1 :(得分:0)
你知道,我已经忍受了这个问题多年,并且无法找到解决方案。第二个我在网上发布它......它告诉了我。发生这种情况时我讨厌它。无论如何,解决方案如下。
由于数据库以及数据对象是使用强类型链接构造的,因此您可以通过执行Permission.User.Property来访问任何给定权限组中引用的用户。您也可以分配给此Permission.User来设置链接。
虽然我之前尝试使用DataDomainSources尝试执行此操作,但它总是给我一个错误,说明该对象已绑定到另一个DataDomainSources。这是因为你有一个DataDomainSources用于Permissions,一个用于Users。
但是,使用DataContext时不存在此限制,因为用户和权限都存在于同一DataContext中。
因此,我重新编写了如下代码:
<navigation:Page x:Class="Chemical_Management_System.EditPermissions"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="EditPermissions Page" Width="640" HorizontalAlignment="Center"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:my="clr-namespace:Chemical_Management_System.Web"
xmlns:myn="clr-namespace:Chemical_Management_System"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" >
<Border BorderBrush="Silver" BorderThickness="1" Height="45" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Border1" Style="{StaticResource HeaderBackground}" VerticalAlignment="Top" Width="618">
<TextBlock Height="23" Name="TextBlock21" Style="{StaticResource HeaderText}" Text="Edit Permissions" />
</Border>
<Button Content="Apply" Height="23" HorizontalAlignment="Left" Margin="553,445,0,0" Name="btnApply" VerticalAlignment="Top" Width="75" />
<Button Content="New" Height="23" HorizontalAlignment="Left" Margin="12,445,0,0" Name="btnNew" VerticalAlignment="Top" Width="75" />
<Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="93,445,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="472,445,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" />
<sdk:DataGrid AutoGenerateColumns="False" Height="364" HorizontalAlignment="Left" Margin="10,75,0,0" Name="dataGrid" VerticalAlignment="Top" Width="618" DataContext="dataContext.Permissions">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="DescriptionColumn" Binding="{Binding Path=Description}" Header="Description" MinWidth="245" />
<sdk:DataGridTemplateColumn Header="User" MinWidth="190">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding User.Name}" Padding="5,0,0,0" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Loaded="UserComboBox_Loaded" SelectionChanged="UserComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Permission" MinWidth="190">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding PermissionLookup.Description}" Padding="5,0,0,0" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Loaded="PermissionComboBox_Loaded" SelectionChanged="PermissionComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
Partial Public Class EditPermissions
Inherits Page
Dim Context As New Web.DataContext()
Dim user As New CurrentUser()
Public Sub New()
InitializeComponent()
AddHandler user.userLoadedCompleted, AddressOf userLoaded
Context.Load(Context.GetPermissionsQuery())
Context.Load(Context.GetUsersQuery())
Context.Load(Context.GetPermissionLookupsQuery)
dataGrid.ItemsSource = Context.Permissions
End Sub
'Executes when the user navigates to this page.
Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
End Sub
Private Sub userLoaded()
If user.permissionCheck("Admin") = False Then
MessageBox.Show("You do not have permission to access this page")
NavigationService.Navigate(New Uri("/Home", UriKind.Relative))
End If
End Sub
Private Sub UserComboBox_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
Dim combo As ComboBox = sender
combo.ItemsSource = Context.Users
End Sub
Private Sub UserComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim combo As ComboBox = sender
Dim user As Web.Users = combo.SelectedItem
Dim currentPermission As Web.Permissions = dataGrid.SelectedItem
currentPermission.User = user
End Sub
Private Sub PermissionComboBox_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
Dim combo As ComboBox = sender
combo.ItemsSource = Context.PermissionLookups
End Sub
Private Sub PermissionComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim combo As ComboBox = sender
Dim permissionLookup As Web.PermissionLookups = combo.SelectedItem
Dim currentPermission As Web.Permissions = dataGrid.SelectedItem
currentPermission.PermissionLookup = permissionLookup
End Sub
Private Sub btnApply_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnApply.Click
Context.SubmitChanges()
End Sub
Private Sub btnNew_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnNew.Click
Dim newpermission As New Web.Permissions
Context.Permissions.Add(newpermission)
End Sub
End Class