开发者

If datagrid hasn't been shown, I can't set in it current position row (silverlight)

i have application with two Tabs. on first tab placed button which sets the current position in dataGrid1 on second tab. While i won't show a second tab, i can't set current position by 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;
        }
    }

}


The problem is that the DataGrid isn't loaded when you attempt to set the SelectedIndex in the button click handler if you haven't already navigated to the tab that contains the DataGrid.

The way to achieve what you want is to use data binding. You will also want to implement INotifyPropertyChanged for any subsequent changes to the property you bind DataGrid.SelectedIndex to. The following is a rough example of how to do what you want in the code you provided.

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

Then update your DataGrid definition in xaml to:

<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜