I have a FlowDocument
(inside a RichTextBox
) that contains UIElement
controls such as CheckBox
es. I need the user to be able to click on the CheckBox
to select it to change the controls properties such as label, background color etc.
The problem I have is that when I click it it only checks, or unchecks the CheckBox
as you would expect. How would I have the CheckBox
display a border around itself when clicked on and not change the checked value. Making the IsEnabled
property false means that I can't even access the control at all, it is not recognised.
I guess the simplest explanation of what I am trying to achieve is similar to Expression Blend, or Visual Studio visual designer. When the user clicks a CheckBox
it gets selected, rather than the checked value toggling.
I have tried searching all over for this but don't know what direction to proceed. Any help would be appreciated.
This answer is not satisfying, but besides building a custom ControlTemplate
, this was the easiest (while probably ugliest) to do.
Assuming we have a CheckBox
by the name of "eins" we would use 3 events to determine its states:
public void eins_Click(object sender, RoutedEventArgs e)
{
var _sender = sender as CheckBox;
if (_sender.Tag == null || _sender.Tag.ToString() != "pressedOnce")
{
_sender.Tag = "pressedOnce";
_sender.IsChecked = false;
testStack.Children.Remove(_sender);
Border border = new Border()
{
BorderThickness = new Thickness(2),
BorderBrush = new SolidColorBrush(Colors.Cyan),
};
border.Child = _sender;
testStack.Children.Insert(0, border);
}
else
{
_sender.IsChecked = true;
}
}
private void eins_LostFocus(object sender, RoutedEventArgs e)
{
var _sender = sender as CheckBox;
_sender.Tag = "";
var border = _sender.Parent as Border;
border.Child = null;
testStack.Children.Remove(border);
testStack.Children.Insert(0,_sender);
}
private void eins_Checked(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer()
{
Interval = new TimeSpan(0,0,1)
};
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if (eins.IsChecked == true)
{
//do whatever you want to do
}
}
Now this is more of a workaround solution and has several drawbacks:
1) you need to be sure the box is never checked for longer than 1 sec, else you need to raise the TimeSpan
2) You always need to know what type of Control is the Checkbox's actual parent
3) You would need to finish checking for bugs, this probably won't work out of the box
4) This is not really build for performance
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.