【问题标题】:Orientation attribute of StackPanel defined as top level component in separate xaml file ignoredStackPanel 的方向属性定义为单独的 xaml 文件中的顶级组件被忽略
【发布时间】:2012-07-20 23:44:24
【问题描述】:

我在一个名为 HPanel.xaml 的文件中定义了一个 StackPanel,如下所示:

<StackPanel Orientation="Horizontal" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

然后,像这样使用 HPanel:

<v:HPanel>
  <Label Content="The label" />
  <TextBox Text="{Binding transporter.Foo, 
    UpdateSourceTrigger=PropertyChanged}" />
</v:HPanel>

会给我一个垂直方向而不是水平方向的堆栈面板。

为什么?

【问题讨论】:

  • 我看不出这种行为的明显原因。您可以发布更多代码吗?就像你如何包含你的文件(资源字典等等......)和你的 HPanel.xaml 类的完整代码(如果可能)
  • HPanel.xaml 只包含该行。将 HPanel 作为子控件的控件没有资源字典。 generic.xaml 中未提及 Stackpanel。 HPanel 在另一个 StackPanel 中。如果我将您在上面看到的包含( 等...)更改为具有 Orientation="Horizo​​ntal" 的 StackPanel,则方向将是正确的。
  • 这里缺少一些东西......“HPanel.xaml 只包含那一行” 编译没有错误。你真的不能发布HPanel.xaml 文件的整个 内容吗?用 xmls:x .... 的东西?我感觉问题在于那些缺少的代码行
  • 你说得对,后面的代码一定有,所以在下面,谢谢你的帮助!

标签: wpf xaml


【解决方案1】:

多田!

我现在已经制作了一个有效的测试用例,如果我有一个代码,堆栈面板就会变得很好。处理属性需要 InitializeComponent。

MainWindow.xaml:

<Window x:Class="wpftestapp.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:v="clr-namespace:wpftestapp.views"
     Title="MainWindow" Height="350" Width="525">
    <v:HPanel>
      <Label Content="a"/>
      <Label Content="b"/>
      <Label Content="c"/>
    </v:HPanel>
</Window>

HPanel.xaml:

<StackPanel Orientation="Horizontal"
    x:Class="wpftestapp.views.HPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/>

HPanel.xaml.cs:

using System.Windows.Controls;

namespace wpftestapp.views
{
  public partial class HPanel : StackPanel
  {
    public HPanel()
    {
      InitializeComponent();
    }
  }
}

感谢您的帮助!

【讨论】:

  • 啊,是的,这确实可以解释。所以我最终想了解更多关于 HPanel 类的信息是完全正确的 ^^ 很高兴它帮助你解决问题。
猜你喜欢
  • 2018-07-04
  • 2017-02-02
  • 2018-01-02
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
相关资源
最近更新 更多