我已经创建了一个附加属性,现在如何使用它?
我正在尝试确定附加行为是否是我们为应用程序中的表单构建控件所需要的。
因此,我不仅想知道如何创建附加行为,而且希望看到它们使用来解决问题的真实实例。我使用 这篇 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个真实但非常简单的示例:当用户单击按钮时显示一条消息的附加行为。可以通过行为本身的依赖属性来自定义消息。
行为代码:
使用行为的 XAML:
这里的关键是您必须重写
OnAttached
和OnDetached
方法。在这里,您可以将处理程序附加/分离到您想要在关联元素中控制的任何事件,并在处理程序中执行您想要的任何操作。这是一种为视觉控件添加交互性的非常强大且灵活的方法。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:
The XAML that uses the behavior:
They key here is that you must override the
OnAttached
andOnDetached
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 theSystem.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)