打开表单时,按钮和单选按钮显示为禁用

时间:2017-04-10 07:58:01

标签: c# xaml mvvm

我不明白一件事。如果LicencePlate不好,我的表格会打开。在这种形式,我有几个按钮,文本框和单选按钮。根据条件启用和禁用这些按钮和单选按钮。

文本框LicencePlate有一段代码,它将禁用或启用这些单选按钮和按钮。

// <summary>
    /// Fill Licence Plate with LicencePlateFinal from NotifiedTruck.
    /// </summary>
    public String LicencePlate
    {
        get { return PlateInformation.LicencePlateFinal; }
        set
        {
            PlateInformation.LicencePlateFinal = value;               

            // Musí volat domeček - pokud se avizovaná a skutečná liší.
            // Call house - if avizo  != LicencePlateNotified
            if (LicencePlate != Avizo)
            {
                _canEnableBtn = false;
                NotifyPropertyChanged("CanEnableButton");
                _canEnableAcpt = true;
                NotifyPropertyChanged("CanEnableAccept");
                _canEnableRadio = true;
                NotifyPropertyChanged("CanEnableRadio");                  
            }

            // Avizovaná se rovná skutečné, ale liší se od OCRkované: povolují nakládku tlačítkem Pokračovat v nakládce která otevře detail vozidla v samostatném formuláři
            // Avizo == LicencePlateFinal , but Avizo != OCR  or LicencePlateFinal != OCR; then you can continue in loading process.
            else if (LicencePlate == Avizo || LicencePlate != PlateInformation.LicencePlateOCR || Avizo != PlateInformation.LicencePlateOCR)
            {
                _canEnableBtn = true;
                NotifyPropertyChanged("CanEnableButton");
                _canEnableAcpt = false;
                NotifyPropertyChanged("CanEnableAccept");
                _canEnableRadio = false;
                NotifyPropertyChanged("CanEnableRadio");                   
            }
            NotifyPropertyChanged("LicencePlate");
        }
    }      

当我打开表格时。所有字段均已禁用。当我点击LicencePlate TextBox并写一些思考时,条件激活。但我希望在打开表单时激活条件。我在LicencePlate文本框中写了一些文字后没有。我还在特定的单选按钮和按钮上绑定更新源触发器。

这是属性集:

public bool CanEnableButton
    {
        get
        {
            return _canEnableBtn;
        }

        set
        {
            if (_canEnableBtn != value)
            {
                _canEnableBtn = value;
                NotifyPropertyChanged("CanEnableButton");
            }
        }
    }

    public bool CanEnableRadio
    {
        get 
        {              

            return _canEnableRadio;
        }

        set
        {
            if (_canEnableRadio != value)
            {
                _canEnableRadio = value;
                NotifyPropertyChanged("CanEnableRadio");
            }
        }
    }

    public bool CanEnableAccept
    {
        get
        {
            return _canEnableAcpt;
        }

        set
        {
            if (_canEnableAcpt != value)
            {
                _canEnableAcpt = value;
                NotifyPropertyChanged("CanEnableAccept");
            }
        }
    }        

这里是单选按钮和按钮的xaml:

<Button x:Name="btnContinueLoading" IsEnabled="{Binding CanEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Command="{Binding ContinueLoadingCmd}"  Style="{StaticResource DialogButton}" Content="POKRAČOVAT V NÁKLADCE" Grid.Column="1" Height="31" Width="181" HorizontalAlignment="Right" Margin="0,11,10,0" VerticalAlignment="Top">

    </Button>

    <RadioButton x:Name="chcxReturnState" Content="VRÁTIT" GroupName="Requests" IsChecked="{Binding IsReturned, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                             IsEnabled="{Binding CanEnableRadio, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1" HorizontalAlignment="Left" Margin="130,22,0,0" Grid.Row="1" VerticalAlignment="Top" Width="61"/>
    <RadioButton x:Name="chcxWaitState" Content="POČKAT" GroupName="Requests" IsChecked="{Binding IsWaiting, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                             IsEnabled="{Binding CanEnableRadio, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,22,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Button x:Name="button" Content="POTVRDIT" Command="{Binding ConfirmRequestCmd, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  IsEnabled="{Binding CanEnableAccept,UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,14,0,0" Grid.Row="2"  Height="31" VerticalAlignment="Top" Width="181" Style="{StaticResource DialogButton}"/>

任何提示?非常感谢。

1 个答案:

答案 0 :(得分:1)

这是

的内容
public bool CanEnableButton
{
    get
    {
        return _canEnableBtn;
    }

    set
    {
       _canEnableBtn = value;
       NotifyPropertyChanged("CanEnableButton");
    }
}

public bool CanEnableRadio
{
    get 
    {              

        return _canEnableRadio;
    }

    set
    {
        _canEnableRadio = value;
        NotifyPropertyChanged("CanEnableRadio");
    }
}

public bool CanEnableAccept
{
    get
    {
        return _canEnableAcpt;
    }

    set
    {
         _canEnableAcpt = value;
         NotifyPropertyChanged("CanEnableAccept");
    }        

在你的构造函数中设置你想要的标志。 这样可行。

假设让你假设你的构造函数

public YourConstructor()
{
 CanEnableAccept=true;
 CanEnableRadio=true;
 CanEnableButton=true;
}