ProgressBar.Value奇怪地影响其他元素的位置

时间:2015-10-23 07:38:37

标签: wpf vb.net xaml binding

除非我遗漏了一些基本内容,否则这似乎是一个错误。它很容易重现。只需创建一个新的WPF应用程序项目,并将以下内容粘贴到MainWindow的XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="100" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
  <Grid>
    <ListBox>
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
          <Setter Property="IsTabStop" Value="False" />
        </Style>
      </ListBox.ItemContainerStyle>
      <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:AudioFile}">
          <Grid HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="3" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="32" />
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Image Width="24" Height="24" VerticalAlignment="Center" HorizontalAlignment="Left">
              <Image.Source>
                <BitmapImage UriSource="{Binding Icon}" />
              </Image.Source>
            </Image>
            <TextBlock Grid.Column="1" Text="{Binding FullPath}" VerticalAlignment="Center" />
            <ProgressBar Grid.Column="1" Opacity=".3" Foreground="LightGreen" Value="{Binding UploadProgress}" />
            <TextBlock Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="8">
              <Run Text="{Binding UploadRate, StringFormat={}{0:00}}" />
              <Run> kbps</Run>
            </TextBlock>
            <Image Grid.Column="2" Width="16" Height="16" VerticalAlignment="Center" />
            <ProgressBar Grid.Row="1" Grid.ColumnSpan="3" Value="{Binding PercentCompleted, Mode=OneWay}" />
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
      <ListBox.Items>
        <local:AudioFile FullPath="C:\audio1.mp3" Status="Uploading" UploadProgress="0" PercentCompleted="40"  ErrorUploading="" />
      </ListBox.Items>
    </ListBox>
  </Grid>
</Window>

添加一个名为AudioFile的新类,并在其中粘贴以下代码:

Public Class AudioFile
  Public Property FullPath() As String
  Public Property UploadProgress() As Integer
  Public Property UploadRate() As Single = 120
  Public Property Status() As String
  Public Property ErrorUploading() As String
  Public ReadOnly Property Icon() As String
    Get
      If String.IsNullOrEmpty(FullPath.Trim()) Then
        Return ""
      Else
        Dim RetVal As String = "Resources/speaker.png"

        Select Case System.IO.Path.GetExtension(FullPath).ToLower().Trim("."c)
          Case "mp3"
            RetVal = "Resources/mp3.png"
          Case "wav"
            RetVal = "Resources/wav.png"
        End Select

        Return RetVal
      End If
    End Get
  End Property
  Public Property PercentCompleted() As Integer
End Class

现在转到XAML并将PercentCompleted的值从"40"更改为"80"。请注意FullPath TextBlock向右跳的位置。为什么?我们对PercentCompleted的唯一约束是ProgressBar的{​​{1}}。

0 个答案:

没有答案