2015-12-30 78 views
1

我有以下XAML代码显示了一组在我的窗口控件:WPF进度不会左对齐正确

<Grid Height="80"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

    <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Width="80" /> 

    <TextBlock Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}" 
       Foreground="#B0B0B0" VerticalAlignment="Top" /> 
    <TextBlock Grid.Column="1" Grid.Row="1" Margin="5,0" Text="{Binding Description}" Foreground="#c9c9c9" 
       VerticalAlignment="Top" /> 
    <ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" /> 

</Grid> 

它基本上是一个图片的左侧和两个的TextBlocks和进度向右。

我的问题是,当我的窗口变宽时,ProgressBar在最后会无限延伸。 我希望我的进度条在某个点停止增长。因此,如果用户拉伸窗口,它将停止在给定的MaxWidth。 所以我想这:

<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200"/> 

但如果它被拉伸比左边和进度的图像之间MaxWidth生成空间更多,这将居中进度窗口的中间。 我希望进度坚持到左侧,但HorizontalAlignment="Left"的进度减少到几个像素:

<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" HorizontalAlignment="Left"/> 

有没有人有一个想法如何使这项工作?

回答

1

你也可以换你ProgressBar定义为低于Grid有两列:

<Grid Grid.Column="1" Grid.Row="2"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" MaxWidth="200"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <ProgressBar Grid.Column="0" Value="50" Margin="5" Height="15"/> 
</Grid> 
1

您可以尝试结合其他元素的宽度:

<TextBlock x:Name="title" Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}" Foreground="#B0B0B0" VerticalAlignment="Top" /> 

<ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" Width="{Binding Path=ActualWidth, ElementName=title}" MaxWidth="200" HorizontalAlignment="Left"/>