I've met a problem when using the VisualState. In the "Common State"VisualStateGroup, there are four VisualStates which are "Normal","Selected","PointerOver","SelectedAndPointerOver".
<VisualState x:Name="Normal"></VisualState><VisualState x:Name="PointerOver"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#007ACC" Duration="0"></ColorAnimation></Storyboard></VisualState><VisualState x:Name="Selected"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#CA5100" Duration="0"></ColorAnimation></Storyboard></VisualState><VisualState x:Name="SelectedAndPointerOver"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#9932CC" Duration="0"></ColorAnimation></Storyboard></VisualState>
And its corresponding transitions.
<VisualTransition To="PointerOver"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#007ACC" Duration="0:0:0.5"></ColorAnimation></Storyboard></VisualTransition><VisualTransition To="Selected"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#CA5100" Duration="0:0:0.5"></ColorAnimation></Storyboard></VisualTransition><VisualTransition To="Normal"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="Transparent" Duration="0:0:0.5"></ColorAnimation></Storyboard></VisualTransition><VisualTransition To="SelectedAndPointerOver"><Storyboard><ColorAnimation Storyboard.TargetName="Brush" Storyboard.TargetProperty="Color" To="#9932CC" Duration="0:0:0.5"></ColorAnimation></Storyboard></VisualTransition>
When I press the button, it changes to the "Selected" state. But when I press another button, the previous one will lose its focus and automatically changes to "Normal" state even ifI did not define this behavior. So I set the "LostFocus" event to update the state of the control to prevent this from happening.
private void LostFocus(object sender, RoutedEventArgs e) { if (!m_select_map[sender as Button])//Check the state VisualStateManager.GoToState(sender as FrameworkElement, "Normal", false); else VisualStateManager.GoToState(sender as FrameworkElement, "Selected", false); }
But this still happens when its top control, theMainWindow, lost its focus.
Can anyone tell me why and how to prevent this from happening.