好吧,這太愚蠢了!我很困惑。我有這個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
不起作用。你有什麼想法嗎?
完善建議:d –
@Javad_Amiry我已經編輯我的答案有一個「雜項」部分,如果它直接應用,這可能是爲你的使用情況較好。只需一個就可以完成你所需要的任務,從而避免使用過多的佈局容器。 – Viv
那麼,表單有許多行 - 問題示例只顯示其中的2個 - 我必須定義大量的RowDefinition。另外,我必須爲每個控件設置'Grid.Row'和'Grid.Column',這意味着如果我想重新定位一行,我必須改變很多控件的位置。但我同意你的看法,「網格」似乎是一個更好的容器。對上述問題有任何想法嗎? –