问题
I have a DataGrid, one column is designed to display download percent. Below is my code:
<DataGridTemplateColumn Header="Percent" IsReadOnly="True" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<Button Margin="5,0,0,0" DockPanel.Dock="Right" VerticalContentAlignment="Center" Style="{StaticResource BasicButtonStyle}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StopButtonClickCommand}" >
<Image Height="12" Source="/Client;component/Resources/Images/stop.png" />
</Button>
<ProgressBar Value="{Binding Path=Percent, Mode=OneWay}" Minimum="0" Maximum="100" />
<TextBlock Text="{Binding Path=Percent , StringFormat={}{0}%}" HorizontalAlignment="Center" ></TextBlock>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I want to display the text on progress bar, at same time keep the button on the right. How should I modify my code?
回答1:
put ProgressBar and TextBlock in one container (Grid)
<Grid>
<ProgressBar Value="{Binding Path=Percent, Mode=OneWay}" Minimum="0" Maximum="100" />
<TextBlock Text="{Binding Path=Percent , StringFormat={}{0}%}" HorizontalAlignment="Center" />
</Grid>
来源:https://stackoverflow.com/questions/49894696/wpf-how-to-display-text-on-progress-bar