简体   繁体   English

在 UWP 应用程序中通过键盘交互修改行的可见性

[英]Modify the visibility of a row via keyboard interaction in UWP application

Here is my scenario.这是我的场景。 My table has fixed number of columns, say 2, and initially, it has only one visible row, but when the focus is on the last column of row 1 and the user press 'tab', row 2 will be made visible.我的表有固定的列数,比如 2,最初它只有一个可见行,但是当焦点位于第 1 行的最后一列并且用户按“tab”时,第 2 行将可见。

My problem is that I can't dynamically select the row I want to make visible because I have to specify its x:Name during compilation.我的问题是我无法动态选择要显示的行,因为我必须在编译期间指定它的x:Name

Below is my current work.下面是我目前的工作。

.xaml file .xaml 文件

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal" x:Name="SP1">
        <TextBox Text="1-1"/>
        <TextBox Text="1-2" KeyDown="showNextLine"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal" x:Name="SP2" Visibility="Collapsed">
        <TextBox Text="2-1"/>
        <TextBox Text="2-2"/>
    </StackPanel>
    <!--the remaining rows...-->
</StackPanel>

.cs file .cs 文件

private int lastRowIndex = 1;

private void showNextLine(object sender, KeyRoutedEventArgs e)
{
    lastRowIndex++;
    string nextLineName = "SP" + lastRowIndex.ToString();
    nextLineName.Visibility = Visibility.Visible; // which causes error because nextLineName is string instead of StackPanel
}

Besides, my current implementation is to create 50 rows and make the last 49 invisible initially, and I am open to any method to group all the TextBox more systematically or flexibly.此外,我目前的实现是创建 50 行并最初使最后 49 行不可见,并且我愿意接受任何方法来更系统或灵活地对所有TextBox进行分组。

Thanks for reading.谢谢阅读。

You could give the parent StackPanel an x:Name or keep a reference to it if you create it dynamically:如果您动态创建它,您可以为父 StackPanel 提供一个 x:Name 或保留对它的引用:

<StackPanel x:Name="root" Orientation="Vertical">
    <StackPanel Orientation="Horizontal" x:Name="SP1">
        <TextBox Text="1-1"/>
        <TextBox Text="1-2" KeyDown="showNextLine"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal" x:Name="SP2" Visibility="Collapsed">
        <TextBox Text="2-1"/>
        <TextBox Text="2-2"/>
    </StackPanel>
    <!--the remaining rows...-->
</StackPanel>

...and then get a reference to a child StackPanel using the Children property and some basic LINQ: ...然后使用 Children 属性和一些基本的 LINQ 获取对子 StackPanel 的引用:

private void showNextLine(object sender, KeyRoutedEventArgs e)
{
    lastRowIndex++;
    string nextLineName = "SP" + lastRowIndex.ToString();
    StackPanel child = root.Children.OfType<StackPanel>().FirstOrDefault(x => x.Name == nextLineName);
    if (child != null)
        child.Visibility = Visibility.Visible;
}

How could I create a StackPanel dynamically?如何动态创建 StackPanel?

Like this:像这样:

var sp = new StackPanel { Name = "SP3", Orientation = Orientation.Horizontal, Visibility = Visibility.Collapsed };
sp.Children.Add(new TextBlock { Text = "3-1" });
var txt = new TextBlock() { Text = "3-2" };
txt.KeyDown += showNextLine;
sp.Children.Add(txt);
root.Children.Add(sp);

I can't think of an easy way to do the first part of this (since you have 50 stack panels), but if you put all of them in a dictionary , then you could update them using just the key.我想不出一个简单的方法来完成第一部分(因为你有 50 个堆栈面板),但是如果你把它们都放在一个dictionary ,那么你可以只使用键来更新它们。

Here's the dictionary part done manually:这是手动完成的字典部分:

Dictionary<int, StackPanel> myStackPanels = new Dictionary<int, StackPanel>();
myStackPanels.Add(1, SP1);
myStackPanels.Add(2, SP2);

Then, here's what ShowNextLine would look like:然后,这是ShowNextLine样子:

private void showNextLine(object sender, KeyRoutedEventArgs e)
{
    lastRowIndex++;
    // Modify the StackPanel whose key is lastRowIndex;
    myStackPanels[lastRowIndex] = Visibility.Visible;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM