如果没有显示datagrid,我无法在其中设置当前位置行(silverlight)

时间:2011-06-27 13:39:26

标签: silverlight select datagrid row visible

我有两个标签的应用程序。在第一个选项卡上放置按钮,在第二个选项卡上设置dataGrid1中的当前位置。虽然我不会显示第二个标签,但我无法通过button1设置当前位置。

<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;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

问题是当您尝试在按钮单击处理程序中设置DataGrid时,如果您尚未导航到包含{{1}的选项卡,则不会加载SelectedIndex }。

实现您想要的方法是使用数据绑定。您还需要为绑定DataGrid的属性的任何后续更改实施INotifyPropertyChanged。以下是如何在您提供的代码中执行所需操作的粗略示例。

DataGrid.SelectedIndex

然后将xaml中的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; } 定义更新为:

DataGrid