如何从 C# 代码设置 XAML 元素?

发布于 2024-12-07 10:22:52 字数 1527 浏览 0 评论 0 原文

所以我有 wpf 应用程序,我的 xaml 看起来像这样:

<Window x:Class="MyTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:MyTest.Layouts"
        xmlns:pl="clr-namespace:Planerator"
        Title="CardView" Topmost="False" AllowsTransparency="True" WindowStyle="None" WindowState="Maximized" ShowInTaskbar="False">
    <Grid>
        <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
            <my:FrontLayout />
        </pl:Planerator>

        <pl:Planerator x:FieldModifier="public" x:Name="BackProjection" Margin="240" >
            <my:BackLayout />
        </pl:Planerator>
    </Grid>
    <Window.Background>
        <SolidColorBrush />
    </Window.Background>
</Window>

有没有办法设置 对于我的 C# 代码中的 Planerators?

编辑: 平面器: http://blogs.msdn.com/b/greg_schechter/archive/2007/10/26/enter-the-planerator-dead-simple-3d-in-wpf-with-a-stupid- name.aspx

而 FrontLayot 和 BackLayout -s 只是简单的 xaml 布局。

EDIT2:我需要从代码中设置它们的原因是我想使用条件编译。因为例如对于 DEBUG,它应该是

So i have wpf application and my xaml looks like this:

<Window x:Class="MyTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:MyTest.Layouts"
        xmlns:pl="clr-namespace:Planerator"
        Title="CardView" Topmost="False" AllowsTransparency="True" WindowStyle="None" WindowState="Maximized" ShowInTaskbar="False">
    <Grid>
        <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
            <my:FrontLayout />
        </pl:Planerator>

        <pl:Planerator x:FieldModifier="public" x:Name="BackProjection" Margin="240" >
            <my:BackLayout />
        </pl:Planerator>
    </Grid>
    <Window.Background>
        <SolidColorBrush />
    </Window.Background>
</Window>

Is there a way to set the <my:FrontLayout /> and <my:FrontLayout /> for the Planerators inside my C# code?

EDIT:
Planerator: http://blogs.msdn.com/b/greg_schechter/archive/2007/10/26/enter-the-planerator-dead-simple-3d-in-wpf-with-a-stupid-name.aspx

And the FrontLayot and BackLayout -s are just simple xaml layouts.

EDIT2: The reason why i need to set them from code is that i want o use conditional compilasion. because for example for DEBUG it should be <my:DebugFrontLayer /> .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

晨曦慕雪 2024-12-14 10:22:52

查看 Planerator 类,您会发现它具有 < code>ContentPropertyAttribute set:

[ContentProperty("Child")]
public class Planerator : FrameworkElement
{
}

这意味着

  <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
    <my:FrontLayout />
  </pl:Planerator> 

实际上是这样做的:

  <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
    <pl:Planerator.Child>
      <my:FrontLayout />
    </pl:Planerator.Child>
  </pl:Planerator>

这也意味着从代码隐藏中,例如在 MyTest 的构造函数中,或者在其他任何地方,您可以执行以下操作来实现 相同:

    public MyTest()
    {
        InitializeComponent();            
        FrontProjection.Child = new FrontLayout();
    }

Looking at the Planerator class you'll see it has the ContentPropertyAttribute set:

[ContentProperty("Child")]
public class Planerator : FrameworkElement
{
}

This means that

  <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
    <my:FrontLayout />
  </pl:Planerator> 

Actually does this:

  <pl:Planerator x:FieldModifier="public" x:Name="FrontProjection" Margin="240" >
    <pl:Planerator.Child>
      <my:FrontLayout />
    </pl:Planerator.Child>
  </pl:Planerator>

Which also means that from code-behind, for example in the constructor of MyTest, or anywhere else really, you could do the following to achieve the same:

    public MyTest()
    {
        InitializeComponent();            
        FrontProjection.Child = new FrontLayout();
    }
缺⑴份安定 2024-12-14 10:22:52

给它们命名,例如

<my:FrontLayout Name="FrontLayout1" />

,然后它们应该可以从 C# 中作为该名称的变量进行访问。

Give them names, e.g.

<my:FrontLayout Name="FrontLayout1" />

and then they should be accessible from C# as variables of that name.

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