I've read so many solutions this problem. every one of them fails to solve my problem. no matter what container I put the control into or what properties I set on the control/container it will not budge. I have a scroll viewer with a control within. I want it to resize with the window when the user resizes it at runtime. all I need is anchor=top, bottom, left, right. I don't understand why this is so elusive in WPF and why container objects and all kinds of property assignments need to be involved to accomplish what a single property can in Forms. but every solution to this problem still results in my control staying at exactly its design time size as the window is resized at runtime. what's the simple way to get a grip on dynamic control sizing in wpf?
我读过很多解决这个问题的方法。他们每个人都不能解决我的问题。无论我将控件放入哪个容器或在控件/容器上设置什么属性,它都不会改变。我有一个带有控件的滚动查看器。当用户在运行时调整窗口的大小时,我希望它与窗口一起调整大小。我只需要锚=顶部,底部,左边,右边。我不明白为什么在WPF中这如此难以理解,为什么需要包含容器对象和所有类型的属性赋值来完成单个属性在表单中的功能。但是,这个问题的每个解决方案仍然导致我的控件在运行时调整窗口大小时保持其设计时的大小。在wpf中,掌握动态控制大小的最简单方法是什么?
5 个解决方案
#1
44
This has caused me grief as well and AlexK helped me see the light. The trick is NOT to set the Height and Width.... Set these to AUTO
and use the MARGIN
to set the size. Set the HORIZONTALALIGNMENT
and VERTICALALIGNMENT
to STRETCH
and then the anchor functionality works.
这也给我带来了悲伤,AlexK帮助我看到了光明。关键是不设置高度和宽度....将这些设置为AUTO并使用空白来设置大小。将水平对齐和垂直对齐设置为拉伸,然后锚点功能开始工作。
#2
5
The control needs to stretch, that's all there should be to it:
控制需要延伸,这就是它应该有的:
<MyControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
Stretch
replaces setting achors to both respective sides.
拉伸取代了两方面的设置。
For help on panels see this overview. Also see the documentation of the layout system.
有关面板上的帮助,请参见本概述。请参见布局系统的文档。
Most controls automatically stretch, if you have a DataGrid it should stretch too, try this sample in Kaxaml, it contains a DataGrid and a TextBlock which shows its size:
大多数控件都会自动拉伸,如果你有一个DataGrid,它也应该拉伸,试试Kaxaml中的这个例子,它包含一个DataGrid和一个显示其大小的TextBlock:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="grid">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Tag}" Header="Occupation"/>
</DataGrid.Columns>
<FrameworkElement Name="Skeet" Tag="Programmer"/>
<FrameworkElement Name="Gravell" Tag="Programmer"/>
<FrameworkElement Name="Steve" Tag="Coffee Getter"/>
</DataGrid>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding ElementName="grid" Path="ActualWidth"/>
<Binding ElementName="grid" Path="ActualHeight"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
If you size the window down the DataGrid's ScrollBars should appear.
如果您在DataGrid的滚动条下面设置窗口大小,那么应该会出现。
#3
4
I assume the control that does not resize is your custom control.
我假设不调整大小的控件是您的自定义控件。
- Use DockPanel as a container.
- 使用DockPanel作为容器。
- Remove explicit Width and Height properties from your control.
- 从控件中删除显式的宽度和高度属性。
If you work in VS2008, then this causes inconvenience, because you control would collapse to the minimal size when viewed in the designer.
如果您在VS2008中工作,那么这将带来不便,因为在设计器中查看时,您的控件将崩溃到最小大小。
Expressions Blend and starting from VS2010 both respect designer namespace, so you can specify design time only control size.
表达式混合和从VS2010开始,都尊重设计名称空间,所以您可以指定设计时间只控制大小。
For that add the following to your control:
为此,请在控件中添加以下内容:
<UserControl ...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="WWW" d:DesignHeight="HHH">
....
</UserControl>
d:DesignWidth and d:DesignHeight specify the design time width and height.
d:设计宽度和d:设计高度指定设计时间宽度和高度。
#4
1
This question is old but, since I found my here I thought I would throw out my solution. The code below is for a tab control with two tabs and each tab contains a control to fill the tab. I removed the explicitly set width and height, and set the horizontal and vertical alignments to auto on all the controls I wanted to resize. The tab control stretches wit the main window. The controls in the tabs stretch to fill the tabs. The information came from the answers here. I just put up a complete example.
这个问题由来已久,但是,既然我在这里找到了我的答案,我就想把它扔掉。下面的代码用于一个带有两个选项卡的选项卡控件,每个选项卡包含一个用于填充选项卡的控件。我删除了显式设置的宽度和高度,并设置了我想要调整大小的所有控件的水平和垂直对齐。制表符控件延伸到主窗口。选项卡中的控件可伸缩以填充选项卡。信息来自这里的答案。我只是举了一个完整的例子。
Hope this is useful to someone.
希望这对某人有用。
<TabControl HorizontalAlignment="Stretch"
Margin="91,0,0,0" Name="tabControl1"
VerticalAlignment="Stretch" >
<TabItem Header="DataGrid" Name="tabItem1">
<Grid>
<DataGrid AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
Name="dgFTPLog"
VerticalAlignment="Stretch"
Margin="6,6,0,0" />
</Grid>
</TabItem>
<TabItem Header="Log" Name="tabItem2">
<Grid>
<TextBox
HorizontalAlignment="Stretch"
Margin="6,6,0,0" Name="txtLog"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
TextChanged="txtLog_TextChanged" />
</Grid>
</TabItem>
</TabControl>
#5
0
I've came across this issues as well. and Attempted at binding to parent controls ActualHeight and ActualWidth properties except this only works if you do it via code behind not by XAML.
我也遇到过这个问题。并试图绑定到父控件的实际高度和实际宽度属性,但只有通过代码而不是XAML才能这样做。
i.e XAML
我。e XAML
<MyParentControl x:name="parentControl" SizeChanged="parentControl_SizeChanged"> <ChildControl x:name=childControl" /> </MyParentControl>
in code beind
在代码中做
private void parentControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
childControl.Height = parentControl.ActualHeight;
childControl.Width = parentControl.ActualWidth;
}
#1
44
This has caused me grief as well and AlexK helped me see the light. The trick is NOT to set the Height and Width.... Set these to AUTO
and use the MARGIN
to set the size. Set the HORIZONTALALIGNMENT
and VERTICALALIGNMENT
to STRETCH
and then the anchor functionality works.
这也给我带来了悲伤,AlexK帮助我看到了光明。关键是不设置高度和宽度....将这些设置为AUTO并使用空白来设置大小。将水平对齐和垂直对齐设置为拉伸,然后锚点功能开始工作。
#2
5
The control needs to stretch, that's all there should be to it:
控制需要延伸,这就是它应该有的:
<MyControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
Stretch
replaces setting achors to both respective sides.
拉伸取代了两方面的设置。
For help on panels see this overview. Also see the documentation of the layout system.
有关面板上的帮助,请参见本概述。请参见布局系统的文档。
Most controls automatically stretch, if you have a DataGrid it should stretch too, try this sample in Kaxaml, it contains a DataGrid and a TextBlock which shows its size:
大多数控件都会自动拉伸,如果你有一个DataGrid,它也应该拉伸,试试Kaxaml中的这个例子,它包含一个DataGrid和一个显示其大小的TextBlock:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="grid">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Tag}" Header="Occupation"/>
</DataGrid.Columns>
<FrameworkElement Name="Skeet" Tag="Programmer"/>
<FrameworkElement Name="Gravell" Tag="Programmer"/>
<FrameworkElement Name="Steve" Tag="Coffee Getter"/>
</DataGrid>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding ElementName="grid" Path="ActualWidth"/>
<Binding ElementName="grid" Path="ActualHeight"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
If you size the window down the DataGrid's ScrollBars should appear.
如果您在DataGrid的滚动条下面设置窗口大小,那么应该会出现。
#3
4
I assume the control that does not resize is your custom control.
我假设不调整大小的控件是您的自定义控件。
- Use DockPanel as a container.
- 使用DockPanel作为容器。
- Remove explicit Width and Height properties from your control.
- 从控件中删除显式的宽度和高度属性。
If you work in VS2008, then this causes inconvenience, because you control would collapse to the minimal size when viewed in the designer.
如果您在VS2008中工作,那么这将带来不便,因为在设计器中查看时,您的控件将崩溃到最小大小。
Expressions Blend and starting from VS2010 both respect designer namespace, so you can specify design time only control size.
表达式混合和从VS2010开始,都尊重设计名称空间,所以您可以指定设计时间只控制大小。
For that add the following to your control:
为此,请在控件中添加以下内容:
<UserControl ...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="WWW" d:DesignHeight="HHH">
....
</UserControl>
d:DesignWidth and d:DesignHeight specify the design time width and height.
d:设计宽度和d:设计高度指定设计时间宽度和高度。
#4
1
This question is old but, since I found my here I thought I would throw out my solution. The code below is for a tab control with two tabs and each tab contains a control to fill the tab. I removed the explicitly set width and height, and set the horizontal and vertical alignments to auto on all the controls I wanted to resize. The tab control stretches wit the main window. The controls in the tabs stretch to fill the tabs. The information came from the answers here. I just put up a complete example.
这个问题由来已久,但是,既然我在这里找到了我的答案,我就想把它扔掉。下面的代码用于一个带有两个选项卡的选项卡控件,每个选项卡包含一个用于填充选项卡的控件。我删除了显式设置的宽度和高度,并设置了我想要调整大小的所有控件的水平和垂直对齐。制表符控件延伸到主窗口。选项卡中的控件可伸缩以填充选项卡。信息来自这里的答案。我只是举了一个完整的例子。
Hope this is useful to someone.
希望这对某人有用。
<TabControl HorizontalAlignment="Stretch"
Margin="91,0,0,0" Name="tabControl1"
VerticalAlignment="Stretch" >
<TabItem Header="DataGrid" Name="tabItem1">
<Grid>
<DataGrid AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
Name="dgFTPLog"
VerticalAlignment="Stretch"
Margin="6,6,0,0" />
</Grid>
</TabItem>
<TabItem Header="Log" Name="tabItem2">
<Grid>
<TextBox
HorizontalAlignment="Stretch"
Margin="6,6,0,0" Name="txtLog"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
TextChanged="txtLog_TextChanged" />
</Grid>
</TabItem>
</TabControl>
#5
0
I've came across this issues as well. and Attempted at binding to parent controls ActualHeight and ActualWidth properties except this only works if you do it via code behind not by XAML.
我也遇到过这个问题。并试图绑定到父控件的实际高度和实际宽度属性,但只有通过代码而不是XAML才能这样做。
i.e XAML
我。e XAML
<MyParentControl x:name="parentControl" SizeChanged="parentControl_SizeChanged"> <ChildControl x:name=childControl" /> </MyParentControl>
in code beind
在代码中做
private void parentControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
childControl.Height = parentControl.ActualHeight;
childControl.Width = parentControl.ActualWidth;
}