嘿,我尝试实现 John 制作的一个类 Watermark .
我被困住了,想知道是否有人可以帮助我....添加了提到的 2 个类(class) 在 wpf 上:
<AdornerDecorator>
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,184,664,0" x:Name="cbVideoDevices" VerticalAlignment="Top" Width="316" Initialized="cbVideoDevices_Initialized" SelectionChanged="cbVideoDevices_SelectionChanged">
<Controls:WatermarkService.Watermark>
<TextBlock>Type here to search text</TextBlock>
</Controls:WatermarkService.Watermark>
</ComboBox>
</AdornerDecorator>
无论我尝试什么,我都会收到错误,因为控件不存在,或者属性不存在。我在他的类(class)中没有收到任何错误,所以我认为引用很好,但在我看来,System.Windows.Control 丢失了......但我找不到它来添加它......
任何帮助,非常感谢。
编辑:在莉兹的帮助下,我让它工作起来,但让任何人知道,谁确实使用了它。
- AdornerDecorator 在所有东西上创建一个盒子......
- 为 AdornerDecorator 创建边距并将其移动到所需位置
- 边距和对齐螺丝与显示水印的位置......
最佳答案
我尝试了这个示例,它对我有用。
但是,我确实注意到以下几点:
这些类没有指定命名空间,因此我为这两个类添加了一个命名空间。就我而言,“水印”。
namespace Watermark
{
public static class WatermarkService
{
...
}
}
“内部”中的 WatermarkAdorner 类,但这不会打扰您,除非它位于不同的程序集 (dll) 中。如果是,则将其“公开”
然后在 xaml 中,我添加了命名空间声明
xmlns:Controls="clr-namespace:Watermark"
那时一切正常。
我的稍微简化的 xaml 看起来像这样:
<AdornerDecorator >
<ComboBox Height="23" x:Name="cbVideoDevices"
Initialized="cbVideoDevices_Initialized"
SelectionChanged="cbVideoDevices_SelectionChanged">
<controls:WatermarkService.Watermark>
<TextBlock>Type here to search text</TextBlock>
</controls:WatermarkService.Watermark>
</ComboBox>
</AdornerDecorator>
除了删除边距和对齐方式之外,它与您的基本相同。
这有帮助吗?
顺便说一句,我不喜欢在组合框中选择某个项目时仍然显示水印,因此我更改了 WatermarkService 中的 Control_Loaded 方法,如下所示:
private static void Control_Loaded(object sender,RoutedEventArgs e)
{
Control control = (Control)sender;
if(ShouldShowWatermark(control))
{
ShowWatermark(control);
}
else
{
RemoveWatermark(control);
}
}
关于c# - 使用 John Myczek 类的水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5397334/