wpf 中的window窗体中 怎么弹出一个 window窗体

heyuan390908582 2011-07-26 05:38:56
wpf 中 是先声明一个 自定义一个window窗体 然后在 弹出么 怎么弄啊 I'm初学者
谢谢 要详细 啊
...全文
651 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
龍过鸡年 2011-07-27
  • 打赏
  • 举报
回复

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// new TextBlock
var block = new TextBlock()
{
Text = "text block: ",
Foreground = Brushes.Blue,
VerticalAlignment = System.Windows.VerticalAlignment.Center
};

// new TextBox
var box = new TextBox()
{
Text = "请输入文本",
Width = 300,
FontSize = 14d,
Foreground = Brushes.Gray,
VerticalAlignment = System.Windows.VerticalAlignment.Center
};

// 为 TextBox 添加 GotFocus/LostFocus 事件
box.GotFocus += (obj, args) =>
{
if (box.Text == "请输入文本")
{
box.Text = "";
box.Foreground = Brushes.Black;
}
};
box.LostFocus += (obj, args) =>
{
if (box.Text == "")
{
box.Text = "请输入文本";
box.Foreground = Brushes.Gray;
}
};

// new Button
var button_test = new Button()
{
Content = "测试",
Padding = new Thickness(25, 2, 25, 2),
};

var button_close = new Button()
{
Content = "关闭",
Padding = new Thickness(25, 2, 25, 2),
Margin = new Thickness(10,0,0,0)
};

// 为关闭按钮添加 Click 事件
button_close.Click += (obj, args) =>
{
var parent = button_close.Parent;

while (parent != null)
{
if (parent is Window)
{
(parent as Window).Close();
break;
}

if (parent is FrameworkElement)
{
parent = (parent as FrameworkElement).Parent;
}
};
};

// 这个 Panel 是两个按钮的 Host
var panel = new StackPanel()
{
Orientation = Orientation.Horizontal,
VerticalAlignment = System.Windows.VerticalAlignment.Center,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center
};

// 将按钮添加到 Panel 中
panel.Children.Add(button_test);
panel.Children.Add(button_close);

// new Grid
var grid = new Grid();

// 定义 Grid 的 Column & Row
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = System.Windows.GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new System.Windows.GridLength(10d) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = System.Windows.GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = System.Windows.GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = new System.Windows.GridLength(10d) });
grid.RowDefinitions.Add(new RowDefinition() { Height = System.Windows.GridLength.Auto });

// 为 Grid 设置附加属性 TextElement.FontFamily & FontSize
TextElement.SetFontSize(grid, 14d);
TextElement.SetFontFamily(grid, new FontFamily("Microsoft YaHei"));

// 分别设置 TextBlock/TextBox/StackPanel 的 Grid.Column/Grid.Row 附加属性
Grid.SetColumn(block, 0);
Grid.SetColumn(box, 2);
Grid.SetColumnSpan(panel, 3);
Grid.SetRow(panel, 2);

grid.Children.Add(block);
grid.Children.Add(box);
grid.Children.Add(panel);

// new Border
var border = new Border()
{
Background= Brushes.LightPink,
BorderBrush = Brushes.SteelBlue,
BorderThickness = new Thickness(1),
SnapsToDevicePixels = true,
Padding = new Thickness(10),
Margin = new Thickness(10),
Child = grid,
};

// new Window
Window w = new Window()
{
Title = "New Window",
SizeToContent = System.Windows.SizeToContent.WidthAndHeight,
SnapsToDevicePixels = true,
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
Content = border // 将 border 作为 Window.Content
};
w.Show();

/*
* 内容模型
* Window
* Border
* Grid
* TextBlock | TextBox
* StackPanel
* Button | Button
*/
}
龍过鸡年 2011-07-27
  • 打赏
  • 举报
回复
比较简单的方法是先做个 Page
然后用 window.Content = page;


NavigationWindow w = new NavigationWindow();
w.ShowsNavigationUI = false; // <-- 不显示导航栏
// w.Navigate(new Uri("pages/helloworld.xaml", UriKind.Relative)); // <-- 自定义的 page
w.Navigate(new Uri("http://www.google.com"));
w.Show();
syx151 2011-07-27
  • 打赏
  • 举报
回复
你可以自己拖好一个窗体,必要时候show出来不就好了
heyuan390908582 2011-07-27
  • 打赏
  • 举报
回复
是的 在新弹出来的的window里 怎么再添加新的控件啊
dys_198102 2011-07-26
  • 打赏
  • 举报
回复
顶 。。。。。。。。。。。。。。。
636f6c696e 2011-07-26
  • 打赏
  • 举报
回复
            Window a = new Window();
a.Title = "Test Window";
a.Show();


你指这样?

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧