silverlight+控件布局

ascap 2010-05-17 04:23:28
LayoutRoot是Grid,我在上面放了几个button,当程序全屏时一些button就可不到了。请问
怎么定位这些控件,使其相对位置不会发生变化。

谢谢先!
...全文
168 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻想多巴胺 2010-06-11
  • 打赏
  • 举报
回复
kntao 2010-05-27
  • 打赏
  • 举报
回复
我记得用sizechanged事件里重新定位。。。
ycproc 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liguo9868 的回复:]

使用 margin 属性实现。
[/Quote]
正解
坐沙发的 我 汗
superming_jann 2010-05-20
  • 打赏
  • 举报
回复
silverlight 中的margin好强大!
布局在自动适应宽、高度方面 感觉比传统的好很多
websco 2010-05-18
  • 打赏
  • 举报
回复
不晓得楼主明白我的意思没,我的意思是
1. 楼主在切换 FullScreen 时自己用代码改变了布局;
2. 楼主对Silverlight的布局(使用Grid时)似乎还不是很了解,关于水平对齐的Left和垂直对齐的Top方式需要学习一下;
3. 楼主对于在Grid的列宽、行高使用*(Star)似乎也是比较模糊,使用*时在Grid的大小发生改变时是会影响到里面的控件的布局的。

另外,我运行楼主的代码,在16:9宽屏的分辨率下,并没有出现有哪个按钮在全屏下看不到了的情况,不知楼主指的是什么情况?
jv9 2010-05-18
  • 打赏
  • 举报
回复
在全屏模式下,可以使用相对值进行位置设置,

<Button x:Name="btnPosition" Margin="44,11,6,22" Content="Position" Grid.Row="1" d:LayoutOverrides="Height"/>


你的代码中Margin属性定义控件的位置容易错位或者被覆盖。在代码中,你对布局又一次进行修改,这样会造成布局异常出现。

推荐楼主参考这个系列文章,对布局进行修改:http://www.silverlightchina.net/html/zhuantixilie/getstart/2010/0409/978.html
ascap 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 websco 的回复:]
楼主的代码乱写一通...

把代码改成下面这样,自己看看是在做些什么

C# code

if (Application.Current.Host.Content.IsFullScreen)
{

btnPosition.Opacity = 1;
btnPosition.Width = Application.Current.Host.Content.Actua……
[/Quote]

谢谢,那怎么保证按钮以前的位置随着全屏变化而相对位置不变呢?
ascap 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 websco 的回复:]
不晓得楼主明白我的意思没,我的意思是
1. 楼主在切换 FullScreen 时自己用代码改变了布局;
2. 楼主对Silverlight的布局(使用Grid时)似乎还不是很了解,关于水平对齐的Left和垂直对齐的Top方式需要学习一下;
3. 楼主对于在Grid的列宽、行高使用*(Star)似乎也是比较模糊,使用*时在Grid的大小发生改变时是会影响到里面的控件的布局的。

另外,我运……
[/Quote]

谢谢,我的程序整个还是比较复杂的,我只是摘录出来了一点。我再仔细研究一下吧。
websco 2010-05-17
  • 打赏
  • 举报
回复
楼主的代码乱写一通...

把代码改成下面这样,自己看看是在做些什么

if (Application.Current.Host.Content.IsFullScreen)
{

btnPosition.Opacity = 1;
btnPosition.Width = Application.Current.Host.Content.ActualWidth;
btnPosition.Height = Application.Current.Host.Content.ActualHeight;
btnFullScreen.Content = string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}", "Normal"
, Application.Current.Host.Content.ActualWidth,Application.Current.Host.Content.ActualHeight
, btnPosition.ActualWidth, btnPosition.ActualHeight);
}



ascap 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wapdos 的回复:]
把上面那个width和height删除 试试 或责调整GRID的width和height
[/Quote]

试过了,还是不行!
ascap 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liguo9868 的回复:]
使用 margin 属性实现。
[/Quote]
麻烦具体一下呀。
ascap 2010-05-17
  • 打赏
  • 举报
回复
贴上我的代码:
xaml文件

<UserControl x:Class="FullScreen.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Margin="0,0,139,119" Background="#FF2B44BE" Height="auto" Width="auto">
<Grid.RowDefinitions>
<RowDefinition Height="0.657*"/>
<RowDefinition Height="0.152*"/>
<RowDefinition Height="0.191*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.25*"/>
<ColumnDefinition Width="0.401*"/>
<ColumnDefinition Width="0.349*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnPosition" Margin="44,11,6,22" Content="Position" Grid.Row="1" d:LayoutOverrides="Height"/>
<Button x:Name="btnFullScreen" HorizontalAlignment="Left" Margin="5,9,0,24" Width="75" Content="FullScreen" Grid.Column="2" Grid.Row="1" d:LayoutOverrides="Height"/>

</Grid>
</UserControl>


cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FullScreen
{
public partial class MainPage : UserControl
{
private double posWidth = 0;
private double posHeight = 0;

public MainPage()
{
InitializeComponent();
posWidth = btnPosition.Width;
posHeight = btnPosition.Height;

Application.Current.Host.Content.FullScreenChanged+=new EventHandler(Content_FullScreenChanged);
btnFullScreen.Click+=new RoutedEventHandler(btnFullScreen_Click);
}

void btnFullScreen_Click(object sender, RoutedEventArgs e)
{
//判断当前是否是全屏模式
if (Application.Current.Host.Content.IsFullScreen)
{
Application.Current.Host.Content.IsFullScreen = false;
}
else
{
Application.Current.Host.Content.IsFullScreen = true;
}

}

void Content_FullScreenChanged(object sender, EventArgs e)
{
//如果是全屏模式,则设置MediaElement对象的宽度和高度为全屏。
if (Application.Current.Host.Content.IsFullScreen)
{

btnPosition.Opacity = 0;
btnPosition.Width = Application.Current.Host.Content.ActualWidth;
btnPosition.Height = Application.Current.Host.Content.ActualHeight;
btnFullScreen.Content = "Normal";
}
else
{
//恢复MediaElement对象的宽度和高度
btnPosition.Opacity = 1;
btnPosition.Width = posWidth;
btnPosition.Height = posHeight;
btnFullScreen.Content = "FullScreen";
}

}
}
}
websco 2010-05-17
  • 打赏
  • 举报
回复
检查你的对齐方式,在Grid的大小发生变化时,对齐方式的不同会导致控件显示位置不同,不用全屏显示,改变IE窗口的大小应就能看得出效果。
wapdos 2010-05-17
  • 打赏
  • 举报
回复
把上面那个width和height删除 试试 或责调整GRID的width和height
liguo9868 2010-05-17
  • 打赏
  • 举报
回复
使用 margin 属性实现。
ascap 2010-05-17
  • 打赏
  • 举报
回复
不行啊,兄弟。请仔细想想,没那么简单。
cpt_adt2010 2010-05-17
  • 打赏
  • 举报
回复
把Grid分成行列,将button放入具体的某行某列中。就像html时的table.(呵呵,你的问题太简单了,我竟然会)

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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