在 Expression Blend、C#、WPF 中将用户控件置于前端

发布于 2024-10-18 10:44:39 字数 301 浏览 6 评论 0原文

我有一个带有网格的窗口。

在此我有一些按钮,其中一个按钮在单击时将创建一个新的“PostIt”,这是我创建的用户控件。

我想做的是单击“PostIt”并将该控件置于所有其他控件之上。

我已经尝试过...

Grid.SetZIndex(sender, value);

这似乎是正确的代码,没有错误,只是控件没有移动:(

问题可能在于单击的代码位于用户控件中,而不是主窗口 cs 文件中。这重要吗?

“PostIt”只是一个带有文本框的边框。

I have a window with a Grid on.

On this I have some buttons, one of which when clicked will create a new 'PostIt' which is a user control I have created.

What I want to do is click on a 'PostIt' and have that control on top of all the others.

I have tried...

Grid.SetZIndex(sender, value);

Which seems to be the correct code, no errors, just not movement of the control :(

The problem may lie in the fact that the code for the click is in the user control and not the mainwindow cs file. Does this matter?

The 'PostIt' is simply a border with a text box in it.

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

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

发布评论

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

评论(2

海螺姑娘 2024-10-25 10:44:40

您是在 PostIt 鼠标单击的处理程序中调用 Grid.SetZIndex(sender, value) 还是在 PostIt 内的控件的处理程序中调用 Grid.SetZIndex(sender, value) ?您设置的值是多少?

这是一个有效的示例:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" MouseUp="UserControl_MouseUp">
    <Grid>

    </Grid>
</UserControl>

  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
    }

    private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
      Panel.SetZIndex(this, Panel.GetZIndex(this) + 2);
    }
  }


<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <local:UserControl1 Background="Green" Margin="40,40,100,100" Panel.ZIndex="0" />
    <local:UserControl1 Background="Red" Margin="140,140,10,10" Panel.ZIndex="1" />
  </Grid>
</Window>

Jogy

Are you calling Grid.SetZIndex(sender, value) in a handler of the PostIt mouse click, or a handler for a control inside the PostIt? What is the value that you are setting?

Here is an example that works:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" MouseUp="UserControl_MouseUp">
    <Grid>

    </Grid>
</UserControl>

  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
    }

    private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
      Panel.SetZIndex(this, Panel.GetZIndex(this) + 2);
    }
  }


<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <local:UserControl1 Background="Green" Margin="40,40,100,100" Panel.ZIndex="0" />
    <local:UserControl1 Background="Red" Margin="140,140,10,10" Panel.ZIndex="1" />
  </Grid>
</Window>

Jogy

-黛色若梦 2024-10-25 10:44:40

这可能不是最好的解决方案,但它对我有用;我正在重新排序两个网格:

GridOnBottom.SetValue(Grid.ZIndexProperty, (int)GridOnTop.GetValue(Grid.ZIndexProperty) + 1);

...将 GridOnBottom 和 GridOnTop 重命名为您要重新排序的对象的实例。当然,这不是最好的解决方案,但它确实有效。

This may not be the best solution, but it's the one that worked for me; I was re-ordering two grids:

GridOnBottom.SetValue(Grid.ZIndexProperty, (int)GridOnTop.GetValue(Grid.ZIndexProperty) + 1);

...with GridOnBottom and GridOnTop renamed to the instances of the objects you're re-ordering. Granted, it's not the best solution, but it works.

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