2016-12-16 64 views
0

我有3个单选按钮在网格我怎么能在网格中设置一个单选按钮,“检查”

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

我想“xrbtNone”如果其他两个是假的进行检查,但由于我没有将'xrbtNone'保存到数据库,当我重新打开未选中的界面时。

如何将其设置为检查其他两个是否未选中?

我试图创建一个属性,但它没有工作,这是我为我的单选按钮当前的代码

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

回答

0

我最终通过设置我的财产像这样固定的:

如此以来,我的“器isChecked”属性设置为绑定,这个条件语句将布尔的值,并返回属性允许按钮函数通常不需要我在数据库中添加一个字段。

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

我不知道我完全undertstood你想要做什么,但你想要的电台如果其他2个按钮未被选中,按钮“xrbtNone”将被检查?

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

这应该有效。

简短说明:

如果2个单选按钮(你需要更改名称)未被选中,it'l的 “xrbtNone” 按钮。不难理解。希望这可以帮助你:)

+0

由于我的单选按钮是在网格中,我无法检查特定的按钮是否被选中或检查特定的按钮,如果它不在网格中,这将工作。 – RickS

+0

啊,gridview。错过了。那么,不知道这样做。 –

0

该解决方案分两部分。首先,你想给你的所有单选按钮GroupName属性,如在https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.groupname%28v=vs.110%29.aspx

的语法解释应该是这样的:

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

这将确保只有一个按钮可以进行检查,并它应该工作,无论gridview。如果没有,那么看看教程的第3步https://www.asp.net/web-forms/overview/data-access/enhancing-the-gridview/adding-a-gridview-column-of-radio-buttons-cs

其次,你可以指定从一开始就检查xrbtNone,包括“xrbtNone.Checked = true;”在你的实例代码中。这将触发Checked事件,但是如果您不希望触发该事件,则可以通过在检查它之前停用单选按钮并在之后重新激活它们来阻止该事件。

+0

我无法将其设置为GroupName,因为在GridView中,如果将其设置为GroupName并且存在多行,那么它将取消选择所有其他行中的所有其他按钮。但生病尝试看看是否将其设置为默认真实将有所帮助。 – RickS

+0

您应该可以为不同的按钮组设置不同的GroupNames。所以,你可能有LanguageGroup,TimeZoneGroup,EthnicityGroup等。如果你这样做,那么我认为对LanguageGroup按钮的更改不会影响任何其他按钮组。希望这不会强制你创建许多你不想分组的按钮组。 – user3685427

+0

问题是,当我添加一个新的行到网格视图,我选择了三个按钮中的一个,第一行中的按钮由于它们被分组而被取消选中,所以我唯一可以控制它们的方式是通过绑定。 – RickS