我想在左侧面板中创建窗口(带有切换按钮),右侧面板中是视图。当我单击切换按钮时,可以看到视图,并隐藏另一个视图。但是我的代码尽管他自己也开放了。 这是我的代码:
<StackPanel Grid.Column="0">
<ToggleButton Name="Button1" Checked="MenuItem_Checked">
<ToggleButton Name="Button2" Checked="MenuItem_Checked">
</StackPanel>
<administration:View1 Grid.Column="1"
Visibility="{Binding ElementName=Button1, Path=IsChecked, UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<administration:View2 Grid.Column="1"
Visibility="{Binding ElementName=Button2, Path=IsChecked, UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
代码背后:
private void MenuItem_Checked(object sender, RoutedEventArgs e)
{
var el = sender as ToggleButton;
if (Equals(_current, el))
return;
_current = el;
foreach (var menuChild in Menu.Children)
{
ToggleButton button = menuChild as ToggleButton;
if (button == null) continue;
if (!Equals(button, _current))
button.IsChecked = false;
}
}
答案 0 :(得分:2)
我想,在循环中将IsChecked设置为false会导致此问题,因为如果您有任何UnChecked定义并在那里设置“_current”,则在第二次迭代期间_current可能会保留未经检查的ToggleButton,在那里放置一个断点并自行检查,你可以通过包括下面的标志来避免这种情况,
bool internalChange = false;
private void MenuItem_Checked(object sender, RoutedEventArgs e)
{
if (!internalChange)
{
var el = sender as ToggleButton;
if (Equals(_current, el))
return;
_current = el;
foreach (var menuChild in Menu.Children)
{
ToggleButton button = menuChild as ToggleButton;
if (button == null)
continue;
if (!Equals(button, _current))
{
internalChange = true;
button.IsChecked = false;
internalChange = false;
}
}
}
}
答案 1 :(得分:1)
这是我的解决方案:
<View1.Resources>
<Style TargetType="View1">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Button1, Path=IsChecked, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</View1.Resources>
答案 2 :(得分:1)
我设计了一个自定义窗口镶边,弹出菜单和切换按钮。当切换按钮IsCheced属性设置为true时,弹出菜单会打开漂亮的动画,并在关闭弹出窗口时关闭。
显示状态 flyout menu is open