我使用转换器填充WPF中隐藏列的剩余空间(将宽度设置为0.0),但实际上,如果可见,我需要比率1.7*
与*
!
如何计算要为转换器设置的实际值?
转换器:
public class ColumnWidthConverter : IValueConverter
{
public object Convert(
object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
var isVisible = (bool) value;
var width = parameter as string == "*"
? new GridLength(1, GridUnitType.Star).Value
: double.Parse(parameter as string);
return isVisible ? width : 0.0;
}
public object ConvertBack(
object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
您可以看到我尝试使用new GridLength(1, GridUnitType.Star).Value
,但这不是正确的方法!
XAML (压缩):
<appf:ViewUserControl.Resources>
<local:ColumnWidthConverter x:Key="ColumnWidthConverter" />
</appf:ViewUserControl.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.7*"/>
<ColumnDefinition Width="{Binding Path=Visible, Converter={StaticResource ColumnWidthConverter}, ConverterParameter=*}"/>
</Grid.ColumnDefinitions>
Visible
只是相应ViewModel中的一个布尔属性!
答案 0 :(得分:0)
有时候很简单:使用字符串代替双精度值
public class Convenience {
private Clock clock = Clock.systemDefaultZone();
/** For testing only. Sets the clock from which to get the time. */
void setClock(Clock clockForTest) {
this.clock = clockForTest;
}
public String getLocalHour24HourClock() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("kk");
ZonedDateTime zdt = ZonedDateTime.now(clock);
return zdt.format(fmt);
}
}