I've a ToggleButton
with this structure:
我有一个这样结构的按钮:
<ToggleButton Focusable="False" Width="19" Height="19" x:Name="tbBtn" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked"/>
Now my goal is check in the ToggleButton_Checked
event if the user has clicked this event manually, or instead I fired this event through code.
现在我的目标是在ToggleButton_Checked事件中检查用户是否手动单击了这个事件,或者我通过代码触发这个事件。
In particular, in some situation I set the IsChecked
property behind code, like this:
特别是,在某些情况下,我将ischeck属性设置在代码后面,如下所示:
tbBtn.IsChecked = true;
Is possible check in the sender:
是否可以向发件人查询:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
//perform check
}
If this event was trigger manually or set behind code? Maybe it's a property in the sender that I don't know about?
如果这个事件是手动触发的还是在代码后面设置的?可能是sender中的一个属性我不知道?
3 个解决方案
#1
0
try Something like:
尝试:
ToggleButton button = sender as ToggleButton;
else
其他的
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if((sender as ToggleButton).IsChecked)
{
// Code for Checked state
}
else
{
// Code for Un-Checked state
}
}
XAML
XAML
<ToggleButton Height="37" HorizontalAlignment="Left" Margin="485.738,254.419,0,0" VerticalAlignment="Top" Width="109" IsEnabled="True" Click="ToggleButton_Click">Timeout</ToggleButton>
#2
0
You can use the click event instead.
您可以使用单击事件代替。
<ToggleButton x:Name ="tbBtn" Content="Test" Click="ToggleButton_Click"></ToggleButton>
Code behind;
代码后面;
public MainWindow()
{
InitializeComponent();
tbBtn.IsChecked = true;
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
var tb = sender as ToggleButton;
if(tb != null)
{
if(tb.IsChecked.HasValue && tb.IsChecked.Value)
{
Debug.WriteLine("AlreadyChecked");
}
}
}
#3
0
Use a class level variable to track it. So, code will be something like
使用类级变量跟踪它。代码是这样的
ClickedFromCode = true;
tbBtn.IsChecked = true;
ClickedFromCode = false;
Same variable can be used for multiple events fired from code. Now catch this boolean within the
相同的变量可以用于从代码中触发的多个事件。现在在
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
//perform check
if (ClickedFromCode)
{ //do something
}
else
{ //do something
}
}
#1
0
try Something like:
尝试:
ToggleButton button = sender as ToggleButton;
else
其他的
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if((sender as ToggleButton).IsChecked)
{
// Code for Checked state
}
else
{
// Code for Un-Checked state
}
}
XAML
XAML
<ToggleButton Height="37" HorizontalAlignment="Left" Margin="485.738,254.419,0,0" VerticalAlignment="Top" Width="109" IsEnabled="True" Click="ToggleButton_Click">Timeout</ToggleButton>
#2
0
You can use the click event instead.
您可以使用单击事件代替。
<ToggleButton x:Name ="tbBtn" Content="Test" Click="ToggleButton_Click"></ToggleButton>
Code behind;
代码后面;
public MainWindow()
{
InitializeComponent();
tbBtn.IsChecked = true;
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
var tb = sender as ToggleButton;
if(tb != null)
{
if(tb.IsChecked.HasValue && tb.IsChecked.Value)
{
Debug.WriteLine("AlreadyChecked");
}
}
}
#3
0
Use a class level variable to track it. So, code will be something like
使用类级变量跟踪它。代码是这样的
ClickedFromCode = true;
tbBtn.IsChecked = true;
ClickedFromCode = false;
Same variable can be used for multiple events fired from code. Now catch this boolean within the
相同的变量可以用于从代码中触发的多个事件。现在在
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
//perform check
if (ClickedFromCode)
{ //do something
}
else
{ //do something
}
}