Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Nullable Checkbox in Blazor.Bootstrap Grid problem

$
0
0

I found an amazing solution to a nullable checkbox here: Blazor- EditForm InputCheckbox nullable bools issue work around

Unfortunately, it only renders as unchecked in a Blazor.Bootstrap Grid, in spite of stepping through the code & seeing that the AdditionalAttributes has [checked,true] in it.

Can anyone help?

TIA

Code for the component

@inherits InputBase<bool?><input type="checkbox"       value="@CurrentValue"       @attributes="AdditionalAttributes"       class="@CssClass"       style="width:15px; height:15px;vertical-align:-2px;"       @onchange="EventCallback.Factory.CreateBinder<bool?>(this,OnChangeAction,this.CurrentValueAsBool)" />@code {    private bool? _CurrentValueAsBool;    private bool? CurrentValueAsBool    {        get        {            if (string.IsNullOrEmpty(CurrentValueAsString))                _CurrentValueAsBool = null;            else            {                if (bool.TryParse(CurrentValueAsString, out bool _currentBool))                    _CurrentValueAsBool = _currentBool;                else                    _CurrentValueAsBool = null;            }            SetCheckBoxCheckedAttribute(_CurrentValueAsBool);            return _CurrentValueAsBool;        }        set        {            _CurrentValueAsBool = value;        }    }    private void SetCheckBoxCheckedAttribute(bool? _currentValueAsBool)    {        bool _isChecked = _currentValueAsBool.HasValue ? _currentValueAsBool.Value : false;        var _attributes = AdditionalAttributes != null ? AdditionalAttributes.ToDictionary(kv => kv.Key, kv => kv.Value) : new Dictionary<string, object>(); ;        if (!_isChecked)        {            _ = _attributes.ContainsKey("checked") ? _attributes["checked"] = false : _attributes.TryAdd("checked", false);        }        else        {            _ = _attributes.ContainsKey("checked") ? _attributes["checked"] = true : _attributes.TryAdd("checked", true);        }        AdditionalAttributes = _attributes;    }    protected override bool TryParseValueFromString(string value, out bool? result, out string validationErrorMessage)    {        validationErrorMessage = null;        if (string.IsNullOrEmpty(value))        {            result = null;        }        else        {            if (bool.TryParse(value, out bool _result))            {                result = _result;            }            else            {                validationErrorMessage = "Unable to parse value!";                result = null;                return false;            }        }        return true;    }    private Action<Nullable<bool>> OnChangeAction { get => (_inputValue) => CurrentValueAsString = _inputValue.HasValue ? _inputValue.Value.ToString() : null; }}

Usage of component

<Grid TItem="SupplierSite"      Data="@Supplier.SupplierSites"      AllowFiltering="false"      AllowPaging="true"      AutoHidePaging="true"      AllowSorting="false"      Responsive="true"      PageSize="5"      EmptyText="No supplier sites have been identified."      Class="full-width"      @ref="gSupplierSites">    ...<GridColumn TItem="SupplierSite"  HeaderText="Needs New FESN Prefix?" PropertyName="NeedsNewPrefix"><NullableBoolCheckBox Value="context.NeedsNewPrefix"                              ValueExpression="() => context.NeedsNewPrefix"                              ValueChanged="@(async (bool? e) => { await OnNeedsNewPrefixChanged(e, context); })"                              disabled="@(!userModel.CanSetNeedsNewPrefix(KeyRecord))" /></GridColumn>    ...</Grid>

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>