2010-04-14 23 views
0

我试图将元素的高度值绑定到Checkbox.IsChecked属性。为什么这不起作用?选中复选框时是否改变高度?

<Window.Resources> 
    <local:BoolToHeightConverter x:Key="BoolToHeightConverter"/> 
</Window.Resources> 

<Button Name="JustBtn" Content="Hello World"/> 
     <CheckBox IsChecked="{Binding ElementName=JustButton, Path=Height, Converter=BoolToHeightConverter}" /> 


[ValueConversion(typeof(Nullable<bool>), typeof(double))] 
public class BoolToHeightConverter : IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return double.NaN; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return null; 
    } 
} 

它甚至没有启动窗口。说: 'IValueConverter'类型没有公共TypeConverter类

+0

double.NaN不是布尔。 – 2010-04-14 20:11:02

回答

1

有几个问题。首先,当您检查CheckBox时,您似乎试图修改Height属性。如果是这种情况,您应该在转换器的ConvertBack方法中实现您的逻辑,并在Binding上指定Mode。其次,你的绑定应该使用StaticResource引用您的转换器:

<CheckBox IsChecked="{Binding ElementName=JustButton, Path=Height, Converter={StaticResource BoolToHeightConverter}, Mode=OneWayToSource}" /> 
0

对不起 - 我的坏:我忘了通过StaticResource的附加转换器。 对不起...