We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Using ButtonAdv controls as radio buttons

Hello,

I'd like to replace my use of togglebuttons as a style for a group of radio buttons, primarily because the togglebuttons just appear as plain rectangles, and use ButtonAdv controls instead. If I just replace the togglebutton with a ButtonAdv I get a design time error: Exception: 'ButtonAdv' TargetType does not match type of element 'RadioButton'.

 Does anyone know of a way to do this?


//converter class
public class EnumToBoolConverter : IValueConverter
{
   #region IValueConverter Members

   public object Convert(object value,
       Type targetType, object parameter,
       System.Globalization.CultureInfo culture)
   {
      if (parameter.Equals(value))
         return true;
      else
         return false;
   }

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

   }
   #endregion

}

public enum CompanyTypes
{
   Type1Comp,
   Type2Comp,
   Type3Comp
}
//my view xaml
<Window x:Class="WpfTestRadioButtons.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTestRadioButtons"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
         <Style BasedOn = "{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type ToggleButton}" x:Key="TButton">
            <Setter Property = "Background" >
               < Setter.Value >
                  < SolidColorBrush Color="Transparent"/>
               </Setter.Value>
            </Setter>
         </Style> 
        <local:EnumToBoolConverter x:Key="EBConverter"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <RadioButton Style="{StaticResource SyncButton}"
   IsChecked = "{Binding Path=Type,
                Converter={StaticResource EBConverter},
                ConverterParameter={x:Static local:CompanyTypes.Type1Comp}}"/>
            <RadioButton Style="{StaticResource SyncButton}"
   IsChecked = "{Binding Path=Type,
                Converter={StaticResource EBConverter},
                ConverterParameter={x:Static local:CompanyTypes.Type2Comp}}"/>
        </StackPanel>
    </Grid>
</Window>

//my view model property
public CompanyTypes Type
{
   get
   {
      return _type;
   }
   set
   {
      _type = value;
      if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("Type"));
   }
}



3 Replies

FP Farjana Parveen Ayubb Syncfusion Team August 14, 2019 12:32 PM UTC

Hi Zeljiko, 
 
Thank you for contacting Syncfusion support. 
 
Based on provided information we understood that when using the Style for RadioButton which is written based on ButtonAdv, you are getting an Exception: 'ButtonAdv' TargetType does not match type of element 'RadioButton'. Since the RadioButton is derived from the ToggleButton you wont get an exception when given as BasedOn is from ToggleButton. 
 
 
 
But when using the Syncfusion ButtonAdv or MS Button as BasedOn for RadioButton, you will get an exception since both are different types and both have different base types. So can you please explain your requirement with RadioButton or ButtonAdv, so that we can provide a solution based on it 
 
Please let us know if you have any concern on this. 
 
Regards, 
Farjana Parveen A 



ZL Zeljko Lazic replied to Farjana Parveen Ayubb August 14, 2019 05:29 PM UTC

Hi Zeljiko, 
 
Thank you for contacting Syncfusion support. 
 
Based on provided information we understood that when using the Style for RadioButton which is written based on ButtonAdv, you are getting an Exception: 'ButtonAdv' TargetType does not match type of element 'RadioButton'. Since the RadioButton is derived from the ToggleButton you wont get an exception when given as BasedOn is from ToggleButton. 
 
 
 
But when using the Syncfusion ButtonAdv or MS Button as BasedOn for RadioButton, you will get an exception since both are different types and both have different base types. So can you please explain your requirement with RadioButton or ButtonAdv, so that we can provide a solution based on it 
 
Please let us know if you have any concern on this. 
 
Regards, 
Farjana Parveen A 


What I'm trying to accomplish is to create a group of ButtonAdv controls, with IsCheckabe properties set to TRUE, that behave like they're a group of mutually exclusive radio buttons. That is, only one can be selected at a time. Clicking a button that isn't checked, unchecks one of the other buttons that's checked. I've attached a sample solution. The problem with the solution is that multiple buttons can be checked.

Attachment: WpfApplication_78f3b88b.zip


DB Dinesh Babu Yadav Syncfusion Team August 15, 2019 11:36 AM UTC

Hi Zeljko, 
 
Thank you for the update. 
 
We have analyzed the reported behavior that our ButtonAdv should work like a RadioButton , that at a time one item should be selected. On analyzed your sample, we have found that all three ButtonAdv is checked , so on clicking each button, it would get selected. In order to restrict this, we have prepared a workaround by implementing a EventToCommand class and attached the command behavior using the AssociatedObject by getting the dependency object of ButtonAdv and bound to the control using Interaction Behavior. It will uncheck the other button , when other button is checked. 
 
Code [XAML] 
 
<syncfusion:ButtonAdv Margin="0,0,1,0" 
IsCheckable="True" 
IsChecked="{Binding Path=Type, Converter={StaticResource EBConverter}, 
ConverterParameter={x:Static vm:Types.ONE}}" 
Grid.Column="0" 
Height="40" 
Label="ONE" 
cal:Message.Attach="[Event Checked] = [Action onechecked()]"> 
 
<!--Invoking the event behaviors--> 
<i:Interaction.Behaviors> 
<vm:EventToCommand Command="{Binding DataContext.Clicked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" EventName="Checked"/> 
</i:Interaction.Behaviors> 
</syncfusion:ButtonAdv> 
 
 
Code:C# 
 
 
public DelegateCommand<object[]> Clicked { get; set; } 
public ShellViewModel() 
{ 
Type = Types.ONE; 
Clicked = new DelegateCommand<object[]>(executeCommandOne); 
 
} 
 
//Executing command 
private void executeCommandOne(object[] obj) 
{ 
ButtonAdv button = obj[0] as ButtonAdv; 
 
if (button.IsChecked) 
{ 
button.IsChecked = false; 
} 
 
} 
 
 
 
Please try this solution and let us know if it is helpful. 
 
Regards 
Dinesh Babu Yadav 


Loader.
Live Chat Icon For mobile
Up arrow icon