【问题标题】:Watermark using John Myczek's Class使用 John Myczek 的类的水印
【发布时间】:2011-03-24 00:04:23
【问题描述】:

嘿,我尝试实现一个类 John madeWatermark

我被困住了,想知道是否有人可以帮助我....添加了提到的 2 个类 在 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>

无论我尝试什么,由于控件不存在或属性不退出,我不断收到错误消息。我在他的课程中没有收到任何错误,所以我认为参考资料很好,但在我看来 System.Windows.Control 丢失了......但我找不到它来添加它......

任何帮助,非常感谢。

编辑:在 Liz 的帮助下,我完成了这项工作,但要让任何人知道,谁在使用它。

  • AdornerDecorator 在所有东西上创建一个盒子.....
  • 为 AdornerDecorator 创建边距并将其移动到所需位置
  • 边距和对齐螺丝与显示水印的位置......

【问题讨论】:

    标签: c# wpf watermark


    【解决方案1】:

    我试过这个例子,它对我有用。

    但是,我确实注意到了以下几点:

    这些类没有指定命名空间,所以我为这两个类添加了一个。在我的例子中是“水印”。

    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);
      }
    }
    

    【讨论】:

    • 不知道 xmlns 所以我添加了它...这就是它现在的样子 xmlns:Controls="clr-namespace:VideoCapture" &lt;ComboBox Height="23" HorizontalAlignment="Right" Margin="0,184,664,0" x:Name="cbVideoDevices" VerticalAlignment="Top" Width="316" Initialized="cbVideoDevices_Initialized" SelectionChanged="cbVideoDevices_SelectionChanged"&gt; &lt;controls:WatermarkService.Watermark&gt; &lt;TextBlock&gt;Type here to search text&lt;/TextBlock&gt; &lt;/controls:WatermarkService.Watermark&gt; &lt;/ComboBox&gt;
    • 但是仍然得到错误控件是未声明的前缀,并且在类型 WatermarkService 中找不到可附加属性 Watermark
    • Nvm,只是 vs2010 有点脾气暴躁,我现在没有错误,谢谢,但仍然没有显示,我会尝试解决这个问题。 wpf 真的很新:P
    • 感谢它适用于新文本框,我得弄清楚为什么它不适用于我的旧文本框。
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 2023-03-06
    • 1970-01-01
    • 2012-02-21
    • 2012-02-14
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多