gpt4 book ai didi

wpf - 如何在 Visual Studio 2010、WPF 中删除控件轮廓?

转载 作者:行者123 更新时间:2023-12-02 08:57:01 24 4
gpt4 key购买 nike

我正在 VS2010 中创建一个 WPF 应用程序。我使用多个层叠的网格控件来执行与选项卡控件类似的功能。网格控件具有不透明(白色)背景。

出现问题的原因是,无论设计窗口中的“前面”有哪个网格,我仍然可以看到顶部面板“后面”的其他网格上所有其他控件的所有轮廓。视觉上极其困惑。

这只发生在设计时。在运行时,一切显示得很好。

如何关闭所有其他控件的轮廓?

下面的屏幕截图显示了我的顶部网格,其中仅包含 4 个文本框和 4 个单选按钮,但显示了其他网格上所有其他控件的轮廓。

alt text

最佳答案

在您不想看到其控件轮廓的任何网格上设置 RenderTransform,例如:

<Grid RenderTransform="1 0 0 1 10000 10000">

您可以使用附加属性来简化此操作,只需编写以下内容即可将网格设置为在隐藏时自动转换:

<Grid my:OutOfThisWorld.WhenHidden="True">

这是代码:

public class OutOfThisWorld : DependencyObject
{
// GoAway
public static bool GetGoAway(DependencyObject obj) { return (bool)obj.GetValue(GoAwayProperty); }
public static void SetGoAway(DependencyObject obj, bool value) { obj.SetValue(GoAwayProperty, value); }
public static readonly DependencyProperty GoAwayProperty = DependencyProperty.RegisterAttached("GoAway", typeof(bool), typeof(OutOfThisWorld), new UIPropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
obj.SetValue(UIElement.RenderTransformProperty,
(bool)e.NewValue ? new TranslateTransform(100000,10000) : null);
}
});

// WhenHidden
public static bool GetWhenHidden(DependencyObject obj) { return (bool)obj.GetValue(WhenHiddenProperty); }
public static void SetWhenHidden(DependencyObject obj, bool value) { obj.SetValue(WhenHiddenProperty, value); }
public static readonly DependencyProperty WhenHiddenProperty = DependencyProperty.RegisterAttached("WhenHidden", typeof(bool), typeof(OutOfThisWorld), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
if((bool)e.NewValue)
BindingOperations.SetBinding(obj, GoAwayProperty,
new Binding("Visibility")
{
RelativeSource=RelativeSource.Self,
Converter = new IsHiddenConverter()
});
}
});

class IsHiddenConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (Visibility)value == Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); }
}
}

工作原理:设置 OutOfThisWorld.WhenHidden 会创建到 OutOfThisWorld.GoAway 属性的绑定(bind),这样只要目标的 Visibility 为“Hidden”,GoAway 属性就为 true。当 GoAway 实际上变为 true 时,就会添加 RenderTransform。

话虽如此,您是否考虑过使用 Visibility=Collapsed 而不是 Visibility=Hidden?可能会更简单。

我还必须强烈赞同威尔的观察,认为这是一个糟糕的设计。你到底有多“坚持”?如果这是政治问题,我为你感到抱歉。但从技术角度来看,使用模板将其重构为真正的选项卡控件应该非常容易。

关于wpf - 如何在 Visual Studio 2010、WPF 中删除控件轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com