选中复选框后更改高度?

时间:2010-04-14 19:56:29

标签: wpf

我正在尝试将元素的高度值绑定到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类

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

相关问题