开发者

Alter height when checkbox selected?

开发者 https://www.devze.com 2022-12-27 02:32 出处:网络
I\'m trying to bind element\'s Height value to Checkbox.IsChecked property. Why that\'s not working? <Window.Resources>

I'm trying to bind element's Height value to Checkbox.IsChecked property. Why that's not working?

<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;
    }
}

It doesn't even initalize the window. Says: 'IValueConverter' type does 开发者_开发技巧not have a public TypeConverter class


There are a couple of problems. First, it looks like you are trying to modify the Height property when the CheckBox is checked. If this is the case, you should implement your logic in the ConvertBack method of the converter, and specify a Mode on the Binding. Secondly, your Binding should use a StaticResource to reference your converter:

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


I'm sorry - my bad: I forgot to attach converter through StaticResource. Sorry guys...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号