【问题标题】:Select element within StackPanel by index C# XAML通过索引 C# XAML 在 StackPanel 中选择元素
【发布时间】:2016-02-20 12:56:54
【问题描述】:

如何通过Stackpanel 索引选择要操作的元素?

早些时候,this 问题已被提出,但该解决方案不适用于窗口应用程序,而仅适用于通用 Windows 应用程序。

【问题讨论】:

  • 哪些方面不起作用?获取索引或其他内容?
  • yourPanel.Children[index_you_need] 有什么问题?你能举例说明你的exact问题吗?

标签: c# xaml windows-applications


【解决方案1】:

您可以访问Children 属性。它返回一个带有索引器的属性,您可以使用索引访问该属性。以下示例在 StackPanel 中包含两个 TextBlock,并在构造函数中将第二个的文本设置为另一个值:

XAML:

<Window x:Class="TestWPF.MainWindow"
        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"
        xmlns:local="clr-namespace:TestWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel x:Name="pnl">
        <TextBlock Text="Hello" />
        <TextBlock Text="World" />
    </StackPanel>
</Window>

构造函数:

public MainWindow()
{
    InitializeComponent();
    var txt = (TextBlock)pnl.Children[1];
    txt.Text = "Moon";
}

通过索引访问child的相关部分是:pnl.Children[index];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 2019-08-13
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多