开发者

How to bind control IsEnable property to some counter

开发者 https://www.devze.com 2023-02-15 20:05 出处:网络
I want to enable some button only when my counter reach some number (let say 1) int questionCounter; public int QuestionCounter

I want to enable some button only when my counter reach some number (let say 1)

int questionCounter;
public int QuestionCounter
{
    get { return questionCounter; }
    set 
    { 
        questionCounter = value;
        if (questionCounter == 1)
            OnPropertyChanged(new PropertyChangedEventArgs("IsEnabled"));
    }
}

<Button Style="{DynamicResource GoBackButton}"
    x:Name="GoBa开发者_开发知识库ck" Click="GoBack_Click" Margin="100,10,0,0" 
            IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" >

and the initial button state is:

{button.IsEnabled = False;}

but i have no success here...

any help please


I suggest to bind to the result of a converter

public class IsEnabledConverter : IValueConverter
{

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Convert.ToBoolean(Convert.ToInt32(value) > 0);
    }

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

And bind it like this

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

[...]

<Button Style="{StaticResource GoBackButton}"
    x:Name="GoBack" Click="GoBack_Click" Margin="100,10,0,0" 
            IsEnabled="{Binding IsEnabled,  Converter={StaticResource isEnabledConverter}}" />
0

精彩评论

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