2012-09-01 104 views
0

我怎样才能做到以下几点:WPF设置网格相对于自动调整大小列的列宽宽度

<Window x:Class="MyClientsWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Login" WindowStartupLocation="CenterScreen" 
    SizeToContent="WidthAndHeight" 
    MaxWidth="800" MaxHeight="600"> 

    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" Name="labelColumn"/> 
     <ColumnDefinition Width="2*" Name="entryColumn"/> 
    </Grid.ColumnDefinitions> 

    <TextBlock Grid.Row="0" Text="First name: " Name="firstNameLabel" 
       Margin="4" VerticalAlignment="Center"/> 
    <TextBox Grid.Row="0" Grid.Column="1" 
      Margin="4" HorizontalAlignment="Stretch" /> 
    <TextBlock Grid.Row="1" Text="Last name: " Name="lastNameLabel" 
       Margin="4" VerticalAlignment="Center"/> 
    <TextBox Grid.Row="1" Grid.Column="1" 
      Margin="4" HorizontalAlignment="Stretch" /> 
    </Grid> 

第二列的宽度必须是两倍的第一,但第一列的宽度是自动的, 并取决于字体系列,字体大小等。 另外,第二列需要在窗口大小调整时拉伸。

回答

1

我不知道这是否是最好的答案或没有,但我已经找到方法2:

1)

<ColumnDefinition Width="*" Name="entryColumn" MinWidth="{Binding 
       ElementName=firstNameLabel, Path=ActualWidth}, 
       Converter={StaticResource MultiplyByTwoConverter}"/> 

2)在代码beihind,Window.Loaded事件处理中:

private void onLoaded(object sender, RoutedEventArgs e) 
{ 
    entryColumn.MinWidth = labelColumn.ActualWidth * 2; 
} 

首先也在设计模式下工作,但其次不。以下设计模式为 ,但不在运行时:

<ColumnDefinition Width="*" Name="entryColumn" MinWidth="{Binding 
       ElementName=labelColumn, Path=ActualWidth}, 
       Converter={StaticResource MultiplyByTwoConverter}"/> 
相关问题