C# 设置元素';s到容器其余部分的宽度
好吧,这太愚蠢了!我很困惑。我有这个C# 设置元素';s到容器其余部分的宽度,c#,wpf,C#,Wpf,好吧,这太愚蠢了!我很困惑。我有这个xaml: <StackPanel Style="{DynamicResource FormPanel}"> <StackPanel> <Label Content="{DynamicResource Label_FirstName}" Target="{Binding ElementName=FirstName}"/>
xaml
:
<StackPanel Style="{DynamicResource FormPanel}">
<StackPanel>
<Label Content="{DynamicResource Label_FirstName}"
Target="{Binding ElementName=FirstName}"/>
<TextBox x:Name="FirstName" />
</StackPanel>
<StackPanel>
<Label Content="{DynamicResource Label_LastName}"
Target="{Binding ElementName=LastName}"/>
<TextBox x:Name="LastName" />
</StackPanel>
<!-- and so one... for each row, I have a StackPanel and a Label and Textbox in it -->
</StackPanel>
这种风格:
<Style x:Key="FormPanel" TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Vertical"/>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Style.Resources>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="Margin" Value="10"/>
<Style.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="140"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<!-- this line doesn't affect -->
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
我想将
TextBox.Width
设置为容器的其余(StackPanel
)宽度。在这种情况下,HorizontalAlignment=Stretch
似乎不起作用。你知道吗?StackPanel
只为子元素分配可用空间以外的空间。您需要的是一个DockPanel
看一看关于同一主题的一些详细解释
您可以将代码修改为以下内容:
<Style x:Key="FormPanel"
TargetType="{x:Type StackPanel}">
<Setter Property="Orientation"
Value="Vertical" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Style.Resources>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="Margin"
Value="10" />
<Setter Property="LastChildFill"
Value="True" />
<Style.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Width"
Value="140" />
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
<Grid Margin="5">
<Grid.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin"
Value="5" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin"
Value="5" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="{DynamicResource Label_FirstName}"
Target="{Binding ElementName=FirstName}" />
<TextBox x:Name="FirstName"
Grid.Row="0"
Grid.Column="1" />
<Label Grid.Row="1"
Grid.Column="0"
Content="{DynamicResource Label_LastName}"
Target="{Binding ElementName=LastName}" />
<TextBox x:Name="LastName"
Grid.Row="1"
Grid.Column="1" />
</Grid>
如果只使用1个布局容器,将提供相同的输出。
StackPanel
仅为子元素分配可用空间以外的空间。您需要的是一个DockPanel
看一看关于同一主题的一些详细解释
您可以将代码修改为以下内容:
<Style x:Key="FormPanel"
TargetType="{x:Type StackPanel}">
<Setter Property="Orientation"
Value="Vertical" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Style.Resources>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="Margin"
Value="10" />
<Setter Property="LastChildFill"
Value="True" />
<Style.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Width"
Value="140" />
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
<Grid Margin="5">
<Grid.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin"
Value="5" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin"
Value="5" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="{DynamicResource Label_FirstName}"
Target="{Binding ElementName=FirstName}" />
<TextBox x:Name="FirstName"
Grid.Row="0"
Grid.Column="1" />
<Label Grid.Row="1"
Grid.Column="0"
Content="{DynamicResource Label_LastName}"
Target="{Binding ElementName=LastName}" />
<TextBox x:Name="LastName"
Grid.Row="1"
Grid.Column="1" />
</Grid>
如果只使用1个布局容器,将提供相同的输出。
StackPanel
将只与其内容一样大
我建议您用
DockPanel
替换内部StackPanel
。DockPanel
的最后一个子项将填充可用空间(除非您显式重写该行为)。StackPanel
的大小与其内容相同
我建议您用
DockPanel
替换内部StackPanel
。DockPanel
的最后一个子项将填充可用空间(除非您显式重写该行为)。@Javad\u Amiry我用一个“Misc”部分编辑了我的答案,如果它直接应用于您的用例,可能会更好。当你只需要一个布局容器就可以做你需要的事情时,这样可以避免你没有太多的布局容器。好吧,表单有很多行-问题示例只显示了其中的两行-我必须定义很多RowDefinition
s。此外,我必须为每个控件设置Grid.Row
和Grid.Column
,这意味着如果我想重新定位一行,我必须更改许多控件的位置。但我同意你的看法,网格
似乎是一个更好的容器。你知道我提到的上述问题吗?@Javad_Amiry如果你有很多行(比如超过4行),我会说尝试使用ItemsControl
,为每个包含我们以前的DockPanel
的项目使用一个模板。这将使您拥有的行完全动态。由于您几乎只需定义模板,并在运行时相应地呈现元素。@Javad_Amiry您还可以查看@Rachel的GridHelper
,以帮助使用动态Grid
s@Javad_Amiry我用一个“杂项”部分编辑了我的答案,如果它直接适用于您的用例,它可能会更好。当你只需要一个布局容器就可以做你需要的事情时,这样可以避免你没有太多的布局容器。好吧,表单有很多行-问题示例只显示了其中的两行-我必须定义很多RowDefinition
s。此外,我必须为每个控件设置Grid.Row
和Grid.Column
,这意味着如果我想重新定位一行,我必须更改许多控件的位置。但我同意你的看法,网格
似乎是一个更好的容器。你知道我提到的上述问题吗?@Javad_Amiry如果你有很多行(比如超过4行),我会说尝试使用ItemsControl
,为每个包含我们以前的DockPanel
的项目使用一个模板。这将使您拥有的行完全动态。由于您几乎只需定义模板,并在运行时相应地呈现元素。@Javad_Amiry您还可以查看@Rachel的GridHelper
,以帮助处理动态Grid