Search code examples
c#.netwpfslidertogglebutton

Failing to Restrict Decimal Points of Slider Value + Set IsChecked on ToggleButtons One at a time


Well folks I am sorry for the weird title but couldnt find a better one. Well I am developing a WPF app. I am working on sliders and ToggleButtons. Its a stupid situation. Let me show you my code:

XAML:

<ToggleButton Name="OneSixBit" Content="16 Bit" />
<ToggleButton Name="ThreeTwoBit" Content="32 Bit" />
<ToggleButton Name="TwentyBit" Content="20 Bit" />
<ToggleButton Name="TwentyFourBit" Content="24 Bit" />

<Label Name="BitDelay" Content="Bit Delay" />
<Slider Minimum="0.0" Maximum="255.0" TickFrequency="1.0" Name="bitdelayslider" />
<TextBox Name="BitDelayValue" IsReadOnly="True" Text="{Binding ElementName=bitdelayslider,Path=Value}" />

I have two issues here:

  1. In Togglebuttons, I want to set the toggle state as checked=true on one button at a time. I.e. if toggle state of Btn1 is checked=true and I click on any other toggle button, it should not check unless i make the checked=false on btn1.

  2. I have a slider which has max value from 0.0 to 255.0 and it should move along with a frequency of 1. Whenever I move the slider I am able to get the display the value on textbox but it shows more than 1 decimal point. I.e. when I move from 0.0, the value shown is 2.3456.... etc. How to make sure only single decimal point should be seen?

Thanks :)


Solution

    1. Use RadioButton instead of ToggleButton, they have the behavior you want (one checked at a time) out of box. You can style them like buttons if you want.

    2. Add a StringFormat to your Binding: {Binding ElementName=bitdelayslider, Path=Value, StringFormat=0.0}.