我已经创建了一个附加属性,现在如何使用它?

发布于 2024-08-07 04:01:32 字数 2156 浏览 2 评论 0原文

我正在尝试确定附加行为是否是我们为应用程序中的表单构建控件所需要的。

因此,我不仅想知道如何创建附加行为,而且希望看到它们使用来解决问题的真实实例。我使用 这篇 MSDN 文章 创建一个带有附加的 UserControl属性以我认为有用的方式,即堆栈面板,其中某些子元素要么突出显示,要么不突出显示。

这个示例运行良好,但是我应该在哪里放置子元素突出显示或不突出显示(例如更改背景颜色)的逻辑?

XAML:

<Window x:Class="TestAttachedProperties2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestAttachedProperties2343"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:ExtendedStackPanel>
            <TextBlock local:ExtendedStackPanel.IsHighlighted="True" Text="text1"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="False" Text="text2"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="True" Text="text3"/>
        </local:ExtendedStackPanel>
    </Grid>
</Window>

代码隐藏:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System;

namespace TestAttachedProperties2343
{
    public partial class ExtendedStackPanel : StackPanel
    {
        public static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.RegisterAttached(
            "IsHighlighted",
            typeof(Boolean),
            typeof(ExtendedStackPanel),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public static void SetIsHighlighted(UIElement element, Boolean value)
        {
            element.SetValue(IsHighlightedProperty, value);
        }
        public static Boolean GetIsHighlighted(UIElement element)
        {
            return (Boolean)element.GetValue(IsHighlightedProperty);
        }

        public ExtendedStackPanel()
        {
            InitializeComponent();
        }
    }
}

I'm trying to determine if Attached Behaviors are something we need in building controls for the forms in our application.

Hence, I not only want to know how to create Attached Behaviors but want to see real instances in which they are used to solve problems. I used this MSDN article to create a UserControl with an attached property in a way that I think would be useful, namely, a stackpanel in which certain child elements are either highlighted or not highlighted.

This example runs fine, but where do I put the logic to highlight or not highlight (e.g. change the background color) of the child elements?

XAML:

<Window x:Class="TestAttachedProperties2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestAttachedProperties2343"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:ExtendedStackPanel>
            <TextBlock local:ExtendedStackPanel.IsHighlighted="True" Text="text1"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="False" Text="text2"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="True" Text="text3"/>
        </local:ExtendedStackPanel>
    </Grid>
</Window>

Code Behind:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System;

namespace TestAttachedProperties2343
{
    public partial class ExtendedStackPanel : StackPanel
    {
        public static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.RegisterAttached(
            "IsHighlighted",
            typeof(Boolean),
            typeof(ExtendedStackPanel),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public static void SetIsHighlighted(UIElement element, Boolean value)
        {
            element.SetValue(IsHighlightedProperty, value);
        }
        public static Boolean GetIsHighlighted(UIElement element)
        {
            return (Boolean)element.GetValue(IsHighlightedProperty);
        }

        public ExtendedStackPanel()
        {
            InitializeComponent();
        }
    }
}

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

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

发布评论

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

评论(1

情场扛把子 2024-08-14 04:01:32

这是一个真实但非常简单的示例:当用户单击按钮时显示一条消息的附加行为。可以通过行为本身的依赖属性来自定义消息。

行为代码:

public class MyBehavior : Behavior<Button>
{
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message",
        typeof(string), typeof(MyBehavior),
        new PropertyMetadata("Default message"));

    public MyBehavior()
        : base()
    { }

    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.Click -= new RoutedEventHandler(AssociatedObject_Click);
    }

    void AssociatedObject_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Message);
    }
}

使用行为的 XAML:

<Button x:Name="MyButton" Content="Click me for custom behavior">
    <i:Interaction.Behaviors>
        <local:MyBehavior Message="Hello from custom behavior"/>
    </i:Interaction.Behaviors>
</Button>

这里的关键是您必须重写 OnAttachedOnDetached 方法。在这里,您可以将处理程序附加/分离到您想要在关联元素中控制的任何事件,并在处理程序中执行您想要的任何操作。这是一种为视觉控件添加交互性的非常强大且灵活的方法。

Update

基本 Behavior<> 类位于 System.Windows.Interactivity.dll 程序集中,该程序集不是 Silverligh 运行时的一部分,但与 Microsoft Expression Blend 3 一起安装。无论如何,该程序集似乎可以自由重新分发(请参见此处:http://social.expression.microsoft.com/Forums/en-US/blend/thread/8523aec4-1a10-4864-8ad4-f95a3627bb4a)

Here is a real altough very simple example: an attached behavior that shows a message when a user clicks a button. The message can be customized via a dependency property in the behavior itself.

The behavior code:

public class MyBehavior : Behavior<Button>
{
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message",
        typeof(string), typeof(MyBehavior),
        new PropertyMetadata("Default message"));

    public MyBehavior()
        : base()
    { }

    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.Click -= new RoutedEventHandler(AssociatedObject_Click);
    }

    void AssociatedObject_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Message);
    }
}

The XAML that uses the behavior:

<Button x:Name="MyButton" Content="Click me for custom behavior">
    <i:Interaction.Behaviors>
        <local:MyBehavior Message="Hello from custom behavior"/>
    </i:Interaction.Behaviors>
</Button>

They key here is that you must override the OnAttached and OnDetached methods. Here you attach/detach handlers to whatever events you want to control in the associated element, and do whatever you want in the handlers. It is a very powerful and flexible way for adding interactivity to visual controls.

Update

The base Behavior<> class is in the System.Windows.Interactivity.dll assembly which is not part of the Silverligh runtime, but is installed with Microsoft Expression Blend 3. It seems anyway that this assembly can be freely redistributed (see here: http://social.expression.microsoft.com/Forums/en-US/blend/thread/8523aec4-1a10-4864-8ad4-f95a3627bb4a)

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