使用 Setter 更新样式触发器中的自定义附加属性

发布于 2024-07-17 01:04:48 字数 1669 浏览 13 评论 0原文

我正在尝试附加属性和样式触发器,希望了解更多信息。 我编写了一个非常简单的带有附加属性的 WPF Windows 应用程序:

  public static readonly DependencyProperty SomethingProperty = 
      DependencyProperty.RegisterAttached(
          "Something", 
          typeof(int), 
          typeof(Window1),
          new UIPropertyMetadata(0));

  public int GetSomethingProperty(DependencyObject d)
  {
      return (int)d.GetValue(SomethingProperty);
  }
  public void SetSomethingProperty(DependencyObject d, int value)
  {
      d.SetValue(SomethingProperty, value);
  }

我试图使用按钮样式部分中定义的属性触发器来更新“Something”附加属性:

  <Window x:Class="TestStyleTrigger.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestStyleTrigger;assembly=TestStyleTrigger"
      Title="Window1" Height="210" Width="190">
      <Window.Resources>
          <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
              <Style.Triggers>
                  <Trigger Property="IsPressed" Value="True">
                      <Setter Property="local:Window1.Something" Value="1" />
                  </Trigger>
              </Style.Triggers>
          </Style>
      </Window.Resources>

      <Button Style="{StaticResource buttonStyle}"></Button>
  </Window>

但是,我不断收到以下编译错误:

错误 MC4003:无法解析样式属性“Something”。 验证所属类型是否为 Style 的 TargetType,或使用 Class.Property 语法指定 Property。 第 10 行位置 29。

我不明白为什么它会给我这个错误,因为我确实在该部分的标记中使用了“Class.Property”语法。 谁能告诉我如何修复这个编译错误?

I was trying out attached properties and style triggers hoping to learn more about it.
I wrote a very simple WPF windows app with an attached property:

  public static readonly DependencyProperty SomethingProperty = 
      DependencyProperty.RegisterAttached(
          "Something", 
          typeof(int), 
          typeof(Window1),
          new UIPropertyMetadata(0));

  public int GetSomethingProperty(DependencyObject d)
  {
      return (int)d.GetValue(SomethingProperty);
  }
  public void SetSomethingProperty(DependencyObject d, int value)
  {
      d.SetValue(SomethingProperty, value);
  }

And I was trying to update the 'Something' attached property with a property trigger defined in the button style section:

  <Window x:Class="TestStyleTrigger.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestStyleTrigger;assembly=TestStyleTrigger"
      Title="Window1" Height="210" Width="190">
      <Window.Resources>
          <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
              <Style.Triggers>
                  <Trigger Property="IsPressed" Value="True">
                      <Setter Property="local:Window1.Something" Value="1" />
                  </Trigger>
              </Style.Triggers>
          </Style>
      </Window.Resources>

      <Button Style="{StaticResource buttonStyle}"></Button>
  </Window>

However, I kept getting following compilation error:

error MC4003: Cannot resolve the Style Property 'Something'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property. Line 10 Position 29.

I can't understand why it gives me this error because I did use the 'Class.Property' syntax in the tag of the section. Can any one tell me how can I fix this compilation error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-07-24 01:04:48

依赖属性的支持方法命名不正确,并且必须是静态的:

public static int GetSomething(DependencyObject d)
{
    return (int)d.GetValue(SomethingProperty);
}

public static void SetSomething(DependencyObject d, int value)
{
    d.SetValue(SomethingProperty, value);
}

此外,您不应在 XAML 的本地 XML NS 映射中指定程序集,因为命名空间位于当前程序集中。 改为这样做:

xmlns:local="clr-namespace:TestStyleTrigger"

Your backing methods for the dependency property are named incorrectly and must be static:

public static int GetSomething(DependencyObject d)
{
    return (int)d.GetValue(SomethingProperty);
}

public static void SetSomething(DependencyObject d, int value)
{
    d.SetValue(SomethingProperty, value);
}

Also, you shouldn't specify the assembly in the local XML NS mapping in the XAML because the namespace is in the current assembly. Do this instead:

xmlns:local="clr-namespace:TestStyleTrigger"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文