我使用转换器来“转换”宽度。我想制作“父”宽度的 10%。所以我使用参数值“0.1”(10%)。但在转换器中,此值将转换为“1.0”。我不知道为什么(见下图)。
百分比转换器.cs
using System;
using System.Windows.Data;
namespace ErdBuilder.Shell.Converter
{
public class PercentageConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
double dblValue = System.Convert.ToDouble(value);
double dblParameter = System.Convert.ToDouble(parameter);
double val = dblValue*dblParameter;
return val;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
CoreStyles.xaml
...
xmlns:converter="clr-namespace:ErdBuilder.Shell.Converter"
...
<converter:PercentageConverter x:Key="PercentageConverter" />
...
<Border x:Name="TrackRectangle" Height="15">
<Border Background="SteelBlue" Width="{Binding Converter={StaticResource PercentageConverter},ElementName=TrackRectangle,Path=ActualWidth,ConverterParameter=0.1}"/>
</Border>