在Expression Blend、C#、WPF中将用户控件置于最前面
我有一扇开着格子的窗户 在这上面,我有一些按钮,单击其中一个按钮将创建一个新的“posit”,这是我创建的用户控件 我想做的是点击一个“posit”,并将该控件置于所有其他控件之上 我试过在Expression Blend、C#、WPF中将用户控件置于最前面,c#,wpf,expression-blend,z-index,blend,C#,Wpf,Expression Blend,Z Index,Blend,我有一扇开着格子的窗户 在这上面,我有一些按钮,单击其中一个按钮将创建一个新的“posit”,这是我创建的用户控件 我想做的是点击一个“posit”,并将该控件置于所有其他控件之上 我试过 Grid.SetZIndex(sender, value); 这似乎是正确的代码,没有错误,只是控制装置没有移动:( 问题可能在于点击的代码在用户控件中,而不是主窗口cs文件中。这有关系吗 “PostIt”只是一个带有文本框的边框。您是在PostIt鼠标单击的处理程序中调用Grid.SetZIndex(se
Grid.SetZIndex(sender, value);
这似乎是正确的代码,没有错误,只是控制装置没有移动:(
问题可能在于点击的代码在用户控件中,而不是主窗口cs文件中。这有关系吗
“PostIt”只是一个带有文本框的边框。您是在PostIt鼠标单击的处理程序中调用Grid.SetZIndex(sender,value),还是在PostIt中调用控件的处理程序?您正在设置的值是什么 下面是一个有效的示例:
<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>
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
}
私有void UserControl_MouseUp(对象发送器,MouseButtonEventArgs e)
{
Panel.SetZIndex(这个,Panel.GetZIndex(这个)+2);
}
}
Jogy这可能不是最好的解决方案,但它对我很有效;我重新订购了两个网格:
GridOnBottom.SetValue(Grid.ZIndexProperty, (int)GridOnTop.GetValue(Grid.ZIndexProperty) + 1);
…将GridOnBottom和GridOnTop重命名为您正在订购的对象的实例。诚然,这不是最好的解决方案,但它可以工作。如果您使用
Grid.SetZIndex
用户控件内部的UserControl
则应使用Grid.SetZIndex(此值)
相反,我不确定我在做什么,但这现在起作用了。我想不同的是,我使用的是“发件人”,而不是像Meleak上面说的那样使用“这个”。