简体   繁体   中英

How can I set a radio button in a grid to “checked”

I have a 3 Radio Buttons in a grid

      <dxg:GridColumn x:Name="xrbtCommodityDescription"
                                                FieldName="CommodityDescription"
                                                Header="Commodity Description"
                                                AllowEditing="True"
                                                HeaderTemplate="{StaticResource bold}"
                                                Width="145" MinWidth="60">
                                    <dxg:GridColumn.CellTemplate>
                                        <DataTemplate>
                                            <RadioButton x:Name="xrbtCommodityDescription" IsChecked="{Binding Path=Value}"  
                                                         Checked="RadioButton_Checked" HorizontalAlignment="Center"/>
                                        </DataTemplate>
                                    </dxg:GridColumn.CellTemplate>
                                </dxg:GridColumn>
                                <dxg:GridColumn x:Name="xrbtCommoditySpanish"
                                                FieldName="CommoditySpanish"
                                                Header="Commodity Description Spanish"
                                                AllowEditing="True"

                                                HeaderTemplate="{StaticResource bold}"
                                                Width="190" MinWidth="60">
                                    <dxg:GridColumn.CellTemplate>
                                        <DataTemplate>
                                            <RadioButton x:Name="xrbtCommoditySpanish" IsChecked="{Binding Path=Value}" 
                                                         Checked="RadioButton_Checked" HorizontalAlignment="Center"/>
                                        </DataTemplate>
                                    </dxg:GridColumn.CellTemplate>
                                </dxg:GridColumn>
                                <dxg:GridColumn x:Name="xrbtNone"    
                                                FieldName="None"
                                                Header="Item Description"
                                                AllowEditing="True"
                                                HeaderTemplate="{StaticResource bold}"
                                                Width="105" MinWidth="40">
                                    <dxg:GridColumn.CellTemplate>
                                        <DataTemplate>
                                            <RadioButton x:Name="xrbtNone" IsChecked="{Binding Path=Value}" 
                                                         Checked="RadioButton_Checked" HorizontalAlignment="Center"/>
                                        </DataTemplate>
                                    </dxg:GridColumn.CellTemplate>
                                </dxg:GridColumn>

I want 'xrbtNone' to be checked if the other two are false, but since I don't have 'xrbtNone' saving to the database, when I reopen the interface it's unselected.

How can I set it to checked if the other two are unselected?

I tried creating a property but it didn't work, this is my current code for my radiobuttons

  private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        try
        {

            var selectedItem = xgrdCustFLDocType.SelectedItem as CustomerFLDocType;
            RadioButton RBT = sender as RadioButton;

            if ((Boolean)RBT.IsChecked && RBT.Name == "xrbtCommodityDescription")
            {
                selectedItem.CommodityDescription = true;
                selectedItem.CommoditySpanish = false;
                selectedItem.None = false;                  
            }

            if ((Boolean)RBT.IsChecked && RBT.Name == "xrbtCommoditySpanish")
            {
                selectedItem.CommoditySpanish = true;
                selectedItem.CommodityDescription = false;
                selectedItem.None = false;                    
            }

            if ((Boolean)RBT.IsChecked && RBT.Name == "xrbtNone")
            {
                selectedItem.CommoditySpanish = false;
                selectedItem.CommodityDescription = false;
                selectedItem.None = true;                
            }
        }
        catch (Exception ex)
        {
            ITTaskList.ErrorHandler("Unable To Select Radio Button.", ex, this.Name);
        }
        finally
        {                
            EnableButtons();
        }
    }       

I'm not sure I fully undertstood what you wanted to do, but you want the radio button "xrbtNone" to be checked if the other 2 buttons aren't checked?

private void CheckButtonIfUnchecked()
{
   if (radioButton1.Checked = false && radioButton2.Checked = false)
   {
      xrbtNone.PerformClick();
   }
}

This should work.

Short explanation:

If the 2 radio buttons (you'll need to change the names) are unchecked, it'l the "xrbtNone" button. Not hard to understand. Hopefully this helps you :)

The solution is in two parts. First, you want to give all your radio buttons the GroupName property, as explained at https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.groupname%28v=vs.110%29.aspx

The syntax should be something like:

<RadioButton x:Name="xrbtCommoditySpanish" IsChecked="{Binding Path=Value}" 
Checked="RadioButton_Checked" Groupname="LanguageRadioButtons" 
HorizontalAlignment="Center"/>

This will ensure that only one of the buttons can be checked, and it should work regardless of the gridview. If not, then take a look at Step 3 of the tutorial at https://www.asp.net/web-forms/overview/data-access/enhancing-the-gridview/adding-a-gridview-column-of-radio-buttons-cs

Second, you can designate that xrbtNone is checked from the start by including "xrbtNone.Checked = true;" in your instantiation code. This will fire the Checked event, but you can prevent that by deactivating the radio buttons before checking it and re-activate them afterwards, if you do not wish to fire that event.

I ended up fixing this by setting up my property like this:

So since my 'ischecked' property is set to binding, this conditional statement sets the bool to a value and returns the property allowing the button to function normally without me having to add a field in the database.

private static readonly PropertyInfo _NoneProperty = RegisterProperty(p => p.None, "None");

    public System.Boolean None
    {
        get
        {
            if (!CommodityDescription && !CommoditySpanish)
            {
                None= true;
            } 

            else
            {
                None= false;
            }
            return GetProperty(_NoneProperty); 
        }
        set
        {
            SetProperty(_NoneProperty, value);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM