我正在创建一个带有TextBlock的ProgressBar来通知用户有关下载的信息,我需要将TextBlock绑定到ProgressBar的值并通过XAML格式化。
像这样:
<TextBlock x:Name="TxtBlock_Download" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3"
TextAlignment="Center"
Foreground="White" Padding="0,2,0,0">
<!--Updates the textbox by using multibinding-->
<TextBlock.Text>
<!--TODO fix the StringFormat -->
<MultiBinding Converter="{StaticResource ResourceKey=kk}" StringFormat="{}{}">
<Binding ElementName="ProgressBar_Download" Path="Value"/>
<Binding ElementName="ProgressBar_Download" Path="Maximum"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
我也使用一个单独的类来连接两个值和最大值
像这样:
class Binding : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0] + "/" + values[1].ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return (value as string).Split('/');
}
}
但每次我尝试使用StringFormat=""
格式化字符串时,它会冻结UI直到下载完成(下载仍在后台运行,但程序已冻结)。
答案 0 :(得分:0)
由于您无法格式化已经转换为字符串的数字,我建议您只需重写转换器(因为您已经有一个)来处理所需的所有格式:
class MyAwesomeProgressConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return ((double)values[0]).ToString("f2") + "/" + ((double)values[1]).ToString("f2");
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Enumerable.Repeat(DependencyProperty.UnsetValue, targetTypes.Length).ToArray()
}
}
此外:
ConvertBack
。抛出异常或返回DependencyProperty.UnsetValue
或Binding.DoNothing
。Binding
- 这可能会导致含糊不清