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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Using ButtonAdv controls as radio buttons

Thread ID:

Created:

Updated:

Platform:

Replies:

146678 Aug 13,2019 08:54 PM UTC Aug 15,2019 11:36 AM UTC WPF 3
loading
Tags: ButtonAdv
Zeljko Lazic
Asked On August 13, 2019 08:54 PM UTC

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



Farjana Parveen Ayubb [Syncfusion]
Replied On 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 


Zeljko Lazic
Replied On 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

Dinesh Babu Yadav [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon