c# - 在 C# 中将文本 block 添加到堆栈面板

标签 c# xaml visual-studio-2012 windows-8 stackpanel

我正在使用此代码将 C# 中的文本 block 写入 Xaml 页面

 Sale.Name = "Price";
  Sale.Text = "$200";
  deatilLayout.Children.Add(Sale);
  Grid.SetRow(Sale, 1);

这只是让文本“$200”出现在页面上。

如何将此文本 block 放置在网格第 2 行的 Stackpanel 中?

请参阅下面的页面布局和我想要文本 block 的位置

<Grid  x:Name="deatilLayout">
    <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>

    </Grid.RowDefinitions>

     <Grid Grid.Row="1" Margin="50 0 0 30" Height="600" Width="1200">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
       <Image x:Name="image" Source="{Binding Image}"/>
       <StackPanel Orientation="Vertical" Grid.column="1">
        <TextBlock Text="{Binding Title}" FontSize="52" TextWrapping="Wrap" />
       **//Place textblock from C# here**
         </StackPanel>
         </Grid>
</Grid>

最佳答案

在将其添加到网格之前,只需将其添加到 StackPanel

StackPanel sp = new StackPanel();
sp.Children.Add(Sale);
deatilLayout.Children.Add(sp);
Grid.SetRow(sp, 2);

关于c# - 在 C# 中将文本 block 添加到堆栈面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931663/

相关文章:

c++ - 直接使用 C++ WinRT 组件或 C++ XAML 项目是否存在任何性能问题?

wpf - HighlightBrushKey 设置在 Windows 7 中不起作用

c++ - 如何在 Visual Studio 2012 中禁用大括号补全?

c# - "ViewModel Behavior"的定义是什么?

c# - 使用LINQ统计值频率

c# - C# 如何使用语句转换为 Try-Finally

javascript - Aurelia npm 安装 : tons of "gyp ERR"

c# - 我如何模拟 UserManager 和 RoleManager 进行单元测试

c# - WPF绑定(bind)改变椭圆的填充颜色

visual-studio - VS 2012/2013 AccessViolationException