我打破了我认为非常简单的事情。我们有一个应用程序,它在面板或边框控件上使用一些高级彩色动画,以在特定条件发生时吸引用户注意。通常,这些条件通过与外部设备通信来识别,并且可以随时发生。所以我希望能够为我的标准IsEnabled或IsFocused属性定义一个边框或stackpanel的触发器,但是我可以任意触发它来形成主窗口的代码隐藏(IsMyArbitraryCondition)。我想它会是这样的:
from collections import Counter
Find_word= raw_input('Write Sentence:')
wordTosearch=['is', 'am']
sentence= Find_word.split()
cnt = Counter(sentence)
for k in sentence:
if k in wordTosearch:
print k, wordTosearch[k]
if cnt[wordTosearch]>1:
print "aggresive"
else:
print "Not agressive"
然后在主窗口的代码隐藏中:
<Border x:Name="Fancy_Border">
<Border.Style>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="BorderThickness" Value="5"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMyArbitraryCondition" Value="true">
<Trigger.EnterActions>
<BeginStoryboard x:Name="Board" Storyboard="{StaticResource ComplicatedAnimation}"/>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
我已经看过附加属性,依赖属性等建议,但到目前为止,我对WPF如何知识的了解水平还不足以成功使用这些示例。似乎必须有一个相对简单的方法,我仍然可以使用XAML样式的方便来定义我的复杂触发器行为,但是除了内置属性(IsMouseOver,IsPressed,IsVisible)之外的东西(任何东西!)都会触发该样式触发器等等)。
编辑(使用UserControl和DataTrigger)
我在下面提供了完整且有效的示例。唯一剩下的问题是我不能将命名组件放在扩展边框控件的内容中。编译器返回:
Fancy_Border.IsMyArbitraryCondition = true;
// ComplicatedAnimation occurs
这是一个问题,因为我没有使用MVVM开发并试图直接操作HighlightBorder中的某些单独控件。您知道的任何变通方法都非常有用!
HighlightBorder.cs
Cannot set Name attribute value 'Example' on element 'TextBlock'. 'TextBlock' is under the scope of element 'HighlightBorder', which already had a name registered when it was defined in another scope.
HighlightBorder.xaml
using System;
using System.Windows;
using System.Windows.Controls;
namespace App
{
public partial class HighlightBorder : Border
{
public HighlightBorder()
{
InitializeComponent();
}
public bool IsActiveHighlight
{
get { return (bool) GetValue(ActiveHighlightProperty); }
set { SetValue(ActiveHighlightProperty, value); }
}
public static readonly DependencyProperty ActiveHighlightProperty =
DependencyProperty.Register("IsActiveHighlight", typeof(bool), typeof(HighlightBorder), new UIPropertyMetadata(null));
}
}
MainWindow.xaml
<Border x:Class="App.HighlightBorder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border.Style>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="BorderThickness" Value="5"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsActiveHighlight, RelativeSource={RelativeSource Mode=Self}}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="EnabledPulse" Storyboard="{DynamicResource PulseAccent}"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="EnabledPulse"/>
<BeginStoryboard x:Name="EnabledClear" Storyboard="{DynamicResource ClearAccent}"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
MainWindow.cs
<local:HighlightBorder x:Name="Grouped_Controls">
<TextBlock> Example Content </TextBlock>
</local:HighlightBorder>