如果数据网格尚未显示,我无法在其中设置当前位置行(silverlight)

发布于 2024-11-17 03:26:32 字数 2874 浏览 2 评论 0原文

我有带有两个选项卡的应用程序。在第一个选项卡上放置按钮,用于设置第二个选项卡上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-11-24 03:26:32

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

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

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文