none
How to use ToggleButton in .NET Core Project RRS feed

  • Question

  • Hello:

    I have one .NET Core project, in which I need one ToggleButton to show the current state of my program, it is it running, then shows “On”, if not, then shows “Off”.

    I found in this article:

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.togglebutton?view=netcore-3.1

    that ToggleButton class should work in .NET core 3.0, 3.1. I am using Visual Studio 2019 Version 16.7.6 on Windows 10 Version 2004 (OS Build 19041.572). I can use .NET core 3.1.

    But I can’t find any code example for .NET core.

    However, I found one WPF example for using ToggleButton, as you can see in the picture.

    I want to create a small test .Net core (Windows Forms) app project to use ToggleButton, so I can decide the size of the button and the color and the text inside the toggle button.

    For example, when the programs begins, I can set the beginning size to the biggest size (like 20) and the initial text to “Off”, then change it to “On” when I click on the toggle button.

    I have no idea how to do this, should I install a nuget package for it? But for which nuget package? And how I can write the code to reach my goal?

    Please advise.

    Saturday, October 17, 2020 6:11 PM

Answers

  • Hi zydjohn,

    Thank you for posting here.

    The ToggleButton in the link is not suitable for Windows Forms, and the ToggleButton in WPF is not good-looking. The ToggleButton in the picture is after modifying the Style in Xaml.

    If you want to use such a control in Windows Forms, you need to customize the control, please refer to the link below.

    Toggle Switch Control in Windows Forms

    Windows Forms ToggleButton

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Monday, October 26, 2020 7:59 PM
    Monday, October 19, 2020 8:21 AM

All replies

  • This control is rather a ToggleSwitch : Toggle switches

    In .NET, I use XAML Islands to host those UWP controls

    The doc says for .NET Core :

    "

    Using XAML Islands in WPF and Windows Forms apps that target the .NET Framework. XAML Islands are supported only in apps that target .NET Core 3.x.

    "


    Sunday, October 18, 2020 6:13 AM
  • Hello:

    I don't agree, according to Microsoft document, ToggleButton should be working in .Net core, read the document: 

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.togglebutton?view=netcore-3.1

    ToggleSwitch is a little different, and I can't use WPF, as there are too many limits, but I have to use .NET core (Windows) App.

    But there is no good documents on how to use ToggleButton in .Net core.

    Sunday, October 18, 2020 8:14 AM
  • The bitmap you posted is ToggleSwitch controls (UWP, not WPF)

    ToggleButton is WPF (Assembly: PresentationFramework.dll = WPF)

    WPF controls can be hosted with ElementHost


    • Edited by Castorix31 Sunday, October 18, 2020 8:29 AM
    Sunday, October 18, 2020 8:29 AM
  • Hello:

    Either you don't read my post, or I didn't express myself clearly enough for you to understand.

    I need some ToggleButton demo code to be used in C# .Net core project, but I can't find any helpful guide, even Microsoft indicated in its document saying that ToggleButton "should" work in .Net Core. Remember that .Net Core now has Windows Forms project type for Visual Studio 2019. 

    I showed you the picture from one WPF project, which uses "SliderButton", here is its class declaration:

    public class SliderButton : System.Windows.Controls.Primitives.ToggleButton

    Why I showed you the picture from one WPF project? Since I could NOT find any working .NET core program using the ToggleButton, so I can only show you the image that I want to use in my .NET core Windows Forms App project. UWP project is even worse than WPF, as it has more restrictions, so I have to use .NET core project.

    Sunday, October 18, 2020 1:30 PM
  • A quick test witn C# .NET Core 3.1 on Windows 10 =>

    Monday, October 19, 2020 8:20 AM
  • Hi zydjohn,

    Thank you for posting here.

    The ToggleButton in the link is not suitable for Windows Forms, and the ToggleButton in WPF is not good-looking. The ToggleButton in the picture is after modifying the Style in Xaml.

    If you want to use such a control in Windows Forms, you need to customize the control, please refer to the link below.

    Toggle Switch Control in Windows Forms

    Windows Forms ToggleButton

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Monday, October 26, 2020 7:59 PM
    Monday, October 19, 2020 8:21 AM
  • Hello:

    Your picture looks good, can you share where it is from?

    Thanks,

    Wednesday, October 21, 2020 5:34 PM
  • Please post questions related to Windows Forms, WPF and/or UWA over in the appropriate UI area of the new Microsoft Q&A forums. They can provide directions for getting the UI working the way you want. This forum is for C#-specific questions.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 22, 2020 1:38 PM
    Moderator