[英]expose Togglebutton ischecked property
I have 2 usercontrols, UC1 & UC2: UC1: have 3 togglebuttons , Button1, Button2, Button3 UC2: have canvas with UI elements. 我有2个用户控件,UC1和UC2:UC1:具有3个切换按钮,Button1,Button2,Button3 UC2:具有带UI元素的画布。 etc: ellipse1 等:ellipse1
I want to hide ellipse1 when Button1.ischecked == true 我想在Button1.ischecked == true时隐藏ellipse1
I exposed the checked status of togglebutton, but i read false all the time. 我暴露了切换按钮的检查状态,但是我一直都读为false。
UC1: UC1:
public ButtonLayout()
{
InitializeComponent();
}
public bool IsToggleChecked
{
get { return (bool)Button1.IsChecked; }
}
UC2: 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. 您需要公开ToggleButtons
的Checked
和UnChecked
事件,并从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. 注意:您可以将来自ToggleButton
的两个事件聚合为一个从UC2处理的ToggleStateChanged
事件。
public event Action<bool> ToggleStateChanged;
public ButtonLayout()
{
InitializeComponent();
ToggleButton1.Checked += (sender, e) => ToggleStateChanged?.Invoke(true);
ToggleButton1.UnChecked += (sender, e) => ToggleStateChanged?.Invoke(false);
}
and UC2: 和UC2:
ButtonLayout buttons = new ButtonLayout();
buttons.ToggleStateChanged += SetVisibility;
with 与
private void SetVisibility(bool isChecked)
{
elip1.Visibility = isChecked? Visibility.Hidden : Visibility.Visisble;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.