2008-09-16 116 views
2

我目前正在玩Silverlight(Beta 2)Datagrid控件。在連接SelectionChanged事件之前,網格會通過單擊標題完美排序。現在,單擊網格時,它將觸發SelectionChanged事件,當我單擊標題進行排序時。有沒有辦法解決?Silverlight - DataGrid控件 - 選擇更改事件干擾排序

在一個半相關主題中,我希望在點擊已選項目時點擊SelectionChanged事件(這樣我可以彈出一個彈出窗口以允許用戶編輯所選值) 。現在,你必須點擊一個不同的值,然後返回到你想要的值,以便它彈出。有另一種方法嗎?

包括我的代碼。

頁:

<UserControl x:Class="WebServicesApp.Page" 
xmlns="http://schemas.microsoft.com/client/2007" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
Width="1280" Height="1024" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> 
<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" x:Name="OurStack" Orientation="Vertical" Margin="5,5,5,5"> 
     <ContentControl VerticalAlignment="Center" HorizontalAlignment="Center"> 
      <StackPanel x:Name="SearchStackPanel" Orientation="Horizontal" Margin="5,5,5,5"> 
       <TextBlock x:Name="SearchEmail" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="Email Address:" Margin="5,5,5,5" /> 
       <TextBox x:Name="InputText" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="150" Height="Auto" Margin="5,5,5,5"/> 
       <Button x:Name="SearchButton" Content="Search" Click="CallServiceButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="Auto" Background="#FFAFAFAF" Margin="5,5,5,5"/> 
      </StackPanel>    
     </ContentControl> 
<Grid x:Name="DisplayRoot" Background="White" ShowGridLines="True" 
       HorizontalAlignment="Center" VerticalAlignment="Center" MaxHeight="300" MinHeight="100" MaxWidth="800" MinWidth="200" 
       ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"> 
<data:DataGrid ItemsSource="{Binding ''}" CanUserReorderColumns="False" CanUserResizeColumns="False" 
          AutoGenerateColumns="False" AlternatingRowBackground="#FFAFAFAF" SelectionMode="Single" 
          HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,5,5,5" x:Name="IncidentGrid" SelectionChanged="IncidentGrid_SelectionChanged"> 
       <data:DataGrid.Columns> 
        <data:DataGridTextColumn DisplayMemberBinding="{Binding Address}" Header="Email Address" IsReadOnly="True" /> <!--Width="150"--> 
        <data:DataGridTextColumn DisplayMemberBinding="{Binding whereClause}" Header="Where Clause" IsReadOnly="True" /> <!--Width="500"--> 
        <data:DataGridTextColumn DisplayMemberBinding="{Binding Enabled}" Header="Enabled" IsReadOnly="True" /> 
</data:DataGrid.Columns> 
      </data:DataGrid> 
     </Grid> 
    </StackPanel> 
    <Grid x:Name="EditPersonPopupGrid" Visibility="Collapsed"> 
     <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.765" Fill="#FF8A8A8A" /> 
     <Border CornerRadius="30" Background="#FF2D1DCC" Width="700" Height="400" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1,1,1,1" BorderBrush="#FF000000"> 
      <StackPanel x:Name="EditPersonStackPanel" Orientation="Vertical" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="650" > 
       <ContentControl> 
        <StackPanel x:Name="EmailEditStackPanel" Orientation="Horizontal"> 
         <TextBlock Text="Email Address:" Width="200" Margin="5,0,5,0" /> 
         <TextBox x:Name="EmailPopupTextBox" Width="200" /> 
        </StackPanel> 
       </ContentControl> 
       <ContentControl> 
        <StackPanel x:Name="AppliesToDropdownStackPanel" Orientation="Horizontal" Margin="2,2,2,0"> 
         <TextBlock Text="Don't send when update was done by:" /> 
         <StackPanel Orientation="Vertical" MaxHeight="275" MaxWidth="350" > 
          <TextBlock x:Name="SelectedItemTextBlock" TextAlignment="Right" Width="200" Margin="5,0,5,0" /> 
          <Grid x:Name="UserDropDownGrid" MaxHeight="75" MaxWidth="200" Visibility="Collapsed" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden" > 
           <Rectangle Fill="White" /> 
           <Border Background="White"> 
            <ListBox x:Name="UsersListBox" SelectionChanged="UsersListBox_SelectionChanged" ItemsSource="{Binding UserID}" /> 
           </Border>          
          </Grid> 
         </StackPanel> 
         <Button x:Name="DropDownButton" Click="DropDownButton_Click" VerticalAlignment="Top" Width="25" Height="25"> 
          <Path Height="10" Width="10" Fill="#FF000000" Stretch="Fill" Stroke="#FF000000" Data="M514.66669,354 L542.16669,354 L527.74988,368.41684 z" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1,1,1,1"/> 
         </Button> 
        </StackPanel> 
       </ContentControl> 
       <TextBlock Text="Where Clause Condition:" /> 
       <TextBox x:Name="WhereClauseTextBox" Height="200" Width="800" AcceptsReturn="True" TextWrapping="Wrap" /> 
       <ContentControl> 
        <StackPanel Orientation="Vertical"> 
         <StackPanel Orientation="Horizontal"> 
          <Button x:Name="TestConditionButton" Content="Test Condition" Margin="5,5,5,5" Click="TestConditionButton_Click" /> 
          <Button x:Name="Save" Content="Save" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Save_Click" /> 
          <Button x:Name="Cancel" Content="Cancel" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Cancel_Click" />      
         </StackPanel> 
         <TextBlock x:Name="TestContitionResults" Visibility="Collapsed" /> 
        </StackPanel> 
       </ContentControl> 
      </StackPanel>    
     </Border>    
    </Grid>  
</Grid> 

而且,當網格的選擇更改時發生的呼叫:

Private Sub IncidentGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 

    If mFirstTime Then 
     mFirstTime = False 
    Else 
     Dim data As SimpleASMX.EMailMonitor = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor) 
     Dim selectedGridItem As SimpleASMX.EMailMonitor = Nothing 
     If IncidentGrid.SelectedItem IsNot Nothing Then 
      selectedGridItem = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor) 
      EmailPopupTextBox.Text = selectedGridItem.Address 
      SelectedItemTextBlock.Text = selectedGridItem.AppliesToUserID 
      WhereClauseTextBox.Text = selectedGridItem.whereClause 
      IncidentGrid.SelectedIndex = mEmailMonitorData.IndexOf(selectedGridItem) 
     End If 
     If IncidentGrid.SelectedIndex > -1 Then 
      EditPersonPopupGrid.Visibility = Windows.Visibility.Visible 
     Else 
      EditPersonPopupGrid.Visibility = Windows.Visibility.Collapsed 
     End If 
    End If 
End Sub 

很抱歉,如果我的代碼是殘酷的,我還在學習的Silverlight 。

回答

4

對我來說,這看起來像一個Silverlight錯誤。我剛剛嘗試過,發生在我身上的事情是,當您單擊列標題時,事件觸發兩次,更糟糕的是,所選項目的索引不會與當前選定的項目保持同步。

我建議使用知識第一次的SelectionChanged火災你解決你的方式,DataGrid的的SelectedItem屬性的值將是

下面是一些示例代碼,至少與這個問題一起生活。你的SelectionChanged邏輯可以在如果子句。

public partial class Page : UserControl 
{ 
    private Person _currentSelectedPerson; 

    public Page() 
    { 
     InitializeComponent(); 

     List<Person> persons = new List<Person>(); 
     persons.Add(new Person() { Age = 5, Name = "Tom" }); 
     persons.Add(new Person() { Age = 3, Name = "Lisa" }); 
     persons.Add(new Person() { Age = 4, Name = "Sam" }); 

     dg.ItemsSource = persons; 
    }  

    private void SelectionChanged(object sender, EventArgs e) 
    { 
     DataGrid grid = sender as DataGrid; 
     if (grid.SelectedItem != null) 
     { 
      _currentSelectedPerson = grid.SelectedItem as Person; 
     } 
     else 
     { 
      grid.SelectedItem = _currentSelectedPerson; 
     } 
    } 
} 

public class Person 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 
} 
0

這個工作,但現在如果我排序兩次,在第一個排序,然後彈出窗口作爲第一個選定的網格項目。如果我關閉彈出網格,然後嘗試第二次排序,它會發生堆棧溢出,並使Firefox崩潰。

我在想,我可能需要重新思考在Silverlight中工作,直到系統變得更加穩定。

感謝您的回答Hovito!在Silverlight的DataGrid

相關問題