简体   繁体   中英

expose Togglebutton ischecked property

I have 2 usercontrols, UC1 & UC2: UC1: have 3 togglebuttons , Button1, Button2, Button3 UC2: have canvas with UI elements. etc: ellipse1

I want to hide ellipse1 when Button1.ischecked == true

I exposed the checked status of togglebutton, but i read false all the time.

UC1:

public ButtonLayout()
        {
            InitializeComponent();

        }

        public bool IsToggleChecked
        {
            get { return (bool)Button1.IsChecked; }

        } 

UC2:

ButtonLayout buttons = new ButtonLayout();
            if (buttons.IsToggleChecked == true)
            {
                elip1.Visibility = Visibility.Hidden;
            }

Please let me know where i am going wrong

You need to expose the Checked and UnChecked events for your ToggleButtons and subscribe to them from UC2. There the eventhandler can hide/show the ellipse.

Note: you could aggregate the two events from the ToggleButton into one ToggleStateChanged event that you handle from UC2.

    public event Action<bool> ToggleStateChanged;

    public ButtonLayout()
    {
        InitializeComponent();
        ToggleButton1.Checked += (sender, e) => ToggleStateChanged?.Invoke(true);
        ToggleButton1.UnChecked += (sender, e) => ToggleStateChanged?.Invoke(false);
    }

and UC2:

    ButtonLayout buttons = new ButtonLayout();
    buttons.ToggleStateChanged += SetVisibility;

with

    private void SetVisibility(bool isChecked)
    {
         elip1.Visibility = isChecked? Visibility.Hidden : Visibility.Visisble;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM