2011-06-27 55 views
1

我有兩個標籤的應用程序。在第一個選項卡上放置按鈕,在第二個選項卡上設置dataGrid1中的當前位置。雖然我不會顯示第二個標籤,但我無法通過button1設置當前位置。如果datagrid沒有顯示,我不能在它當前位置設置行(silverlight)

<UserControl x:Class="SilverlightApplication9.MainPage" 
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" 
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <sdk:TabControl Height="234" HorizontalAlignment="Left" Margin="52,44,0,0" Name="tabControl1" VerticalAlignment="Top" Width="326"> 
     <sdk:TabItem Header="tabItem1" Name="tabItem1"> 
      <Grid> 
       <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="74,44,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
      </Grid> 
     </sdk:TabItem> 
     <sdk:TabItem Header="tabItem2" Name="tabItem2"> 
      <Grid> 
       <sdk:DataGrid ItemsSource="{Binding strs}" RowBackground="White" AutoGenerateColumns="False" Height="141" HorizontalAlignment="Left" Margin="36,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="199"> 
        <sdk:DataGrid.Columns> 
         <sdk:DataGridTextColumn Binding="{Binding}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" /> 
        </sdk:DataGrid.Columns> 
       </sdk:DataGrid> 
      </Grid> 
     </sdk:TabItem> 
    </sdk:TabControl> 
</Grid> 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 

namespace SilverlightApplication9 
{ 
    public partial class MainPage : UserControl 
    { 
     private ObservableCollection<string> _strs = new ObservableCollection<string>(); 
     public ObservableCollection<string> strs { get { return _strs; } set { _strs = value; } } 
     public MainPage() 
     { 
      this.DataContext = this; 
      InitializeComponent(); 
      strs.Add("1"); 
      strs.Add("2"); 
      strs.Add("3"); 
      strs.Add("4"); 
      strs.Add("5"); 
     } 
     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      dataGrid1.SelectedIndex = 2; 
     } 
    } 

} 

回答

0

的問題是,DataGrid未加載當您嘗試設置按鈕單擊處理SelectedIndex如果您尚未導航到包含DataGrid標籤。

實現你想要的東西的方法是使用數據綁定。您還需要執行INotifyPropertyChanged,以便對綁定DataGrid.SelectedIndex的物業進行後續更改。以下是如何在您提供的代碼中執行所需內容的粗略示例。

public partial class MainPage : UserControl, INotifyPropertyChanged 
{ 
    private ObservableCollection<string> _strs 
             = new ObservableCollection<string>(); 
    public ObservableCollection<string> strs 
    { 
     get { return _strs; } 
     set { _strs = value; } 
    } 

    public MainPage() 
    { 
     this.DataContext = this; 
     InitializeComponent(); 
     strs.Add("1"); 
     strs.Add("2"); 
     strs.Add("3"); 
     strs.Add("4"); 
     strs.Add("5"); 

     SelectedIndex = 0; 
    } 

    private int _selectedIndex; 
    public int SelectedIndex 
    { 
     get { return _selectedIndex; } 
     set 
     { 
      _selectedIndex = value; 
      var pChanged = PropertyChanged; 
      if (pChanged != null) 
       pChanged(this, new PropertyChangedEventArgs("SelectedIndex")); 
     } 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     SelectedIndex ++; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

然後在XAML更新您DataGrid定義:

<sdk:DataGrid ItemsSource="{Binding strs}" 
       SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" 
       RowBackground="White" 
       AutoGenerateColumns="False" 
       Height="141" 
       HorizontalAlignment="Left" 
       Margin="36,12,0,0" 
       Name="dataGrid1" 
       VerticalAlignment="Top" 
       Width="199"> 
    <sdk:DataGrid.Columns> 
     <sdk:DataGridTextColumn Binding="{Binding}" 
           CanUserReorder="True" 
           CanUserResize="True" 
           CanUserSort="True" 
           Width="Auto" /> 
    </sdk:DataGrid.Columns> 
</sdk:DataGrid> 
+0

非常感謝,這是幫助第一個解決方案 – alexhome

相關問題