Windows phone 7 绑定文本框';s到其父容器的宽度';s实际宽度
我正在以编程方式将Windows phone 7 绑定文本框';s到其父容器的宽度';s实际宽度,windows-phone-7,binding,textbox,width,actualwidth,Windows Phone 7,Binding,Textbox,Width,Actualwidth,我正在以编程方式将文本框和按钮加载到水平堆栈面板。按钮的大小(只包含一个图像)是固定的,但我无法让文本框填充其父级的可用宽度。这是代码的样子: StackPanel parent = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top, }
文本框
和按钮
加载到水平堆栈面板
。按钮的大小(只包含一个图像
)是固定的,但我无法让文本框填充其父级的可用宽度。这是代码的样子:
StackPanel parent = new StackPanel() {
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
};
TextBox textbox = new TextBox() {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
//MinWidth = 375,
};
Button btn = new Button() {
Content = new Image() {
MaxHeight = 40,
MaxWidth = 40,
MinHeight = 40,
MinWidth = 40,
Margin = new Thickness( 0 ),
Source = new BitmapImage(
new Uri( "btnimage.png", UriKind.Relative ) ),
},
HorizontalAlignment = HorizontalAlignment.Right,
BorderBrush = new SolidColorBrush( Colors.Transparent ),
Margin = new Thickness( 0 ),
};
btn.Click += ( ( s, e ) => OnBtnClicked( s, e, textbox ) );
parent.Children.Add( textbox );
parent.Children.Add( btn );
如果我取消注释文本框的MinWidth
设置,它将按我所希望的方式显示,但我不想显式指定宽度。我尝试按如下方式添加绑定,但根本不起作用(文本框消失了!)
提前谢谢你的帮助 正确的答案是不要这样做。看到我的答案,同样的想法也适用于Windows Phone上的Silverlight 在您的示例中,应该使用
DockPanel
<toolkit:DockPanel>
<Button toolkit:DockPanel.Dock="Right" />
<TextBox /> <!-- fill is implied -->
</toolkit:DockPanel>
正确的答案是不要这样做。看到我的答案,同样的想法也适用于Windows Phone上的Silverlight
在您的示例中,应该使用DockPanel
<toolkit:DockPanel>
<Button toolkit:DockPanel.Dock="Right" />
<TextBox /> <!-- fill is implied -->
</toolkit:DockPanel>
不要使用StackPanel
(它总是尽量保持元素尽可能小,以便它们都适合),而是使用网格。然后你可以这样做:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <!-- Note "*" means to use the rest of the space -->
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"/>
<Button Grid.Column="1">
<Button.Content>
<Image .../>
</Button.Content>
</Button>
</Grid>
如果愿意,您可以将其转换为代码而不是XAML,XAML只是更容易在此处快速键入。而不是使用StackPanel
(它总是尽量保持元素尽可能小,以便它们都适合),使用网格。然后你可以这样做:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <!-- Note "*" means to use the rest of the space -->
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"/>
<Button Grid.Column="1">
<Button.Content>
<Image .../>
</Button.Content>
</Button>
</Grid>
如果愿意,您可以将其转换为代码而不是XAML,XAML只是更容易在此处快速键入。这看起来像我需要的,但我在Silverlight Windows Phone工具包中找不到DockPanel
。我只是尝试重新安装它,以确保我有最新的版本,但仍然没有运气。在决定使用工具包组件时应小心。这些将增加Xap的总体尺寸,这可能是WP7的一个重要考虑因素。如果添加工具箱dll的唯一原因是为了让这一功能正常工作,那么应该寻找一个替代的非工具箱解决方案。非常好,Anthony。如果您还没有使用Silverlight工具包,那么我将使用网格。但是拥有DockPanel对于各种布局都非常有用。您几乎总是可以使用网格来进行此类布局,但正如您在Austin的回答中所看到的,基于网格的布局可能会变得非常冗长。这看起来像我需要的,但我在Silverlight Toolkit for Windows Phone中找不到DockPanel
。我只是尝试重新安装它,以确保我有最新的版本,但仍然没有运气。在决定使用工具包组件时应小心。这些将增加Xap的总体尺寸,这可能是WP7的一个重要考虑因素。如果添加工具箱dll的唯一原因是为了让这一功能正常工作,那么应该寻找一个替代的非工具箱解决方案。非常好,Anthony。如果您还没有使用Silverlight工具包,那么我将使用网格。但是拥有DockPanel对于各种布局都非常有用。您几乎可以始终使用网格进行此类布局,但正如您在Austin的回答中所看到的,基于网格的布局可能会变得非常冗长。