简体   繁体   English

将用户控件置于Expression Blend,C#,WPF中

[英]Bringing a user control to the front in Expression Blend, C#, WPF

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. 在此按钮上,我有一些按钮,单击其中一个按钮将创建一个新的“ PostIt”,这是我创建的用户控件。

What I want to do is click on a 'PostIt' and have that control on top of all the others. 我想要做的是单击“ PostIt”,并将该控件置于所有其他控件之上。

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. 问题可能在于单击的代码在用户控件中,而不在mainwindow cs文件中。 Does this matter? 这有关系吗?

The 'PostIt' is simply a border with a text box in it. “ PostIt”只是一个带有文本框的边框。

Are you calling Grid.SetZIndex(sender, value) in a handler of the PostIt mouse click, or a handler for a control inside the PostIt? 您是在PostIt鼠标单击的处理程序中,还是PostIt内部控件的处理程序中调用Grid.SetZIndex(sender,value)? 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 欢乐

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. ...将GridOnBottom和GridOnTop重命名为您要重新排序的对象的实例。 Granted, it's not the best solution, but it works. 当然,这不是最好的解决方案,但是它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM