!我正在使用 Wpf。我想显示两个面板,如果一个是可见的,另一个应该隐藏。两者应处于相同的边距。下面的图片将详细解释我的问题。
谢谢,安妮
谁能指导我如何编码?
尝试这样的事情......
newButton.Click += (o, ev) =>
{
panelA.Visibility = System.Windows.Visibility.Hidden;
panelB.Visibility = System.Windows.Visibility.Visible;
}
closeButton.Click += (o, ev) =>
{
panelB.Visibility = System.Windows.Visibility.Hidden;
panelA.Visibility = System.Windows.Visibility.Visible;
};
我没有VS,所以应该是这样的
<Button x:Name="ToggleButton" Click="ToggleButton_Click"></Button>
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if (Panel1.Visibility == System.Windows.Visibility.Visible)
{
Panel2.Visibility = System.Windows.Visibility.Visible;
Panel1.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
Panel2.Visibility = System.Windows.Visibility.Collapsed;
Panel1.Visibility = System.Windows.Visibility.Visible;
}
}
代码非常简单:
Panel1.Visibility = System.Windows.Visibility.Hidden;
Panel2.Visibility = System.Windows.Visibility.Visible;
试试这个(假设您使用的是 MVVM 模式)
将两个堆栈面板的可见性与 2 个公共属性(布尔)绑定。并在两者的设置属性上,检查其可见性是否==true,然后将其他控件的可见性设置为false。
使用以下可见性转换器
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool bValue = (bool)value;
if (bValue)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
在 xaml 上,您应该将此堆栈面板的可见性绑定为
Visibility="{Binding Panel1Visibility,Converter={StaticResource BoolToVisibilityConverter}}"
使用此可见性转换器
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool bValue = (bool)value;
if (bValue)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
简单地说 panel1.hide() 和 panel2.show