如何允许 Silverlight 弹出窗口调整其内容大小但阻止其增长?
我正在创建一个对 MVVM 更友好的 Silverlight 对话框类。 ChildWindow
类有一些尴尬,这使得它很难从视图模型进行管理。
这意味着我需要声明我自己的用户界面。我已准备好所有传统元素,包括 Popup
控件和不透明度为 50% 的覆盖层。打开/关闭行为也起作用。
我遇到的问题是我的控件模板无法阻止对话框主体响应用户在 TextBox
中的输入而增长。当字符数量超出默认大小时,整个对话框主体会水平拉伸。显然这不是正常行为。
我想要完成的是一种让对话框主体根据内容调整自身大小的方法,但不会根据需要更多或更少空间的子控件而扩展或收缩。这就是 ChildWindow
控件的行为方式,但我已经检查过 其模板,我无法确定它是如何工作的。
我认为我看到了扩展行为,因为我的模板基于 Grid,它允许子控件按照其请求的大小进行设置。我尝试了 StackPanel、ScrollViewer 和其他布局的几种排列,但没有成功。有谁对 ChildWindow
如何实现其静态、内容大小的布局有一些了解,或者可能有其他建议?
这是我到目前为止的模板,精简到最基本的内容:
<Popup>
<Grid x:Name="overlay">
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
...Title...
</Grid>
<Grid Grid.Row="1">
<ContentControl Content="{TemplateBinding DialogBody}" />
</Grid>
</Grid>
</Border>
</Grid>
</Popup>
当 DialogBody
包含 TextBox
或任何其他改变布局大小的内容(例如隐藏控件)时, Popup
的全部内容都会增长。我不想诉诸静态宽度和高度,因为我希望对话框自动调整对话框主体的大小,但只是最初。
I am creating a Silverlight dialog class which is more MVVM-friendly. The ChildWindow
class has some awkwardness which makes it difficult to manage from a view model.
This means I need to declare my own user interface. I have all of the traditional elements in place, including a Popup
control and an overlay with 50% opacity. The open/close behavior is also working.
The problem I am having is that my control template doesn't prevent the dialog body from growing in response to the user typing into a TextBox
. When the amount of characters grows beyond the default size, the entire dialog body stretches horizontally. Obviously this is not normal behavior.
What I would like to accomplish is a way for the dialog body to size itself appropriately to the content, but not expand or contract in response to child controls which require more or less space. This is how the ChildWindow
control behaves, but I have examined its template and I can't determine how that works.
I think I am seeing the expansion behavior because my template is based on Grid
s, which allow child controls as much size as they request. I have tried several permutations of StackPanel
, ScrollViewer
, and other layouts with no success. Does anyone have some insight into how ChildWindow
accomplishes its static, content-sized layout, or perhaps another suggestion?
Here is the template I have so far, stripped down to the bare essentials:
<Popup>
<Grid x:Name="overlay">
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
...Title...
</Grid>
<Grid Grid.Row="1">
<ContentControl Content="{TemplateBinding DialogBody}" />
</Grid>
</Grid>
</Border>
</Grid>
</Popup>
When DialogBody
contains a TextBox
or anything else which changes the layout size, such as hidden controls, the entire content of the Popup
grows. I don't want to resort to a static width and height, because I want the dialog to automatically size to the dialog body, but only initially.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是你想要的......但是
你为什么不注册“overlay”的 Loaded 事件,当 Load 将触发时,相应地设置“overlay”ActualHeight 和 ActualWidth 的高度和宽度。
通过这种方式,控件将获得最初想要的大小,之后就不会改变。
编辑:
您可以使用它以声明方式执行此操作。
Not sure that this is wat you want..., but
wyh don't you register for Loaded event of "overlay" nad when Load will fire set the heigh and width of "overlay" ActualHeight and ActualWidth accordingly.
In this way control will get the size it want initially and wont change after that.
EDIT:
you can use this to do this in declarative way.