如果数据网格尚未显示,我无法在其中设置当前位置行(silverlight)
我有带有两个选项卡的应用程序。在第一个选项卡上放置按钮,用于设置第二个选项卡上 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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,如果您尚未导航到包含
DataGrid
的选项卡,则当您尝试在按钮单击处理程序中设置SelectedIndex
时,不会加载DataGrid
代码>DataGrid。实现您想要的目的的方法是使用数据绑定。您还需要针对绑定
DataGrid.SelectedIndex
的属性的任何后续更改实现INotifyPropertyChanged
。以下是如何在您提供的代码中执行您想要的操作的粗略示例。然后将 xaml 中的
DataGrid
定义更新为:The problem is that the
DataGrid
isn't loaded when you attempt to set theSelectedIndex
in the button click handler if you haven't already navigated to the tab that contains theDataGrid
.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 bindDataGrid.SelectedIndex
to. The following is a rough example of how to do what you want in the code you provided.Then update your
DataGrid
definition in xaml to: