【问题标题】:How to add the Behaviors in Style如何在样式中添加行为
【发布时间】:2017-02-03 02:29:20
【问题描述】:

当我在 XAML 中添加 Interaction.Behaviors 的样式时,出现如下错误。实际上,我只是想添加通用样式,它将在每个视图中使用。

那么,我应该添加更多吗?

“行为”属性没有可访问的设置器。

“行为”属性不是 DependencyProperty。要在标记中使用,非附加属性必须在具有可访问实例属性“行为”的目标类型上公开。对于附加属性,声明类型必须提供静态“GetBehaviors”和“SetBehaviors”方法。

我在这一行有错误。

<Setter Property="e:Interaction.Behaviors">
    <Setter.Value>

这是我所有的示例源代码。

<Window.Resources>
    <Style x:Key="ToolTipTouchStyle" TargetType="{x:Type TextBlock}">
        <!--<Setter Property="e:Interaction.Behaviors" Value="localToolTip:ToolTipTouchScreen" />-->
        <Style.Setters>
            <Setter Property="e:Interaction.Behaviors">
                <Setter.Value>
                    <localToolTip:ToolTipTouchScreen />
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</Window.Resources>

public class ToolTipTouchScreen : Behavior<UIElement>
{

    Timer timer { get; set; }
    ToolTip toolTip { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();

        timer = new Timer();
        timer.Interval = 5000;
        timer.Elapsed += OnTimerElapsed;

        AssociatedObject.MouseLeave += OnMouseLeave;
        AssociatedObject.MouseLeftButtonUp += OnMouseLeftButtonUp;

    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.MouseLeave -= OnMouseLeave;
        AssociatedObject.MouseLeftButtonUp -= OnMouseLeftButtonUp;
    }

    public void OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        CloseToolTip();
    }

    public void OnMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (((dynamic)sender).ToolTip is string)
        {
            toolTip = new ToolTip();
            toolTip.Content = ((dynamic)sender).ToolTip;

        }
        else
        {
            toolTip = (ToolTip)((dynamic)sender).ToolTip;
        }
        
        
        toolTip.IsOpen = true;
        timer.Start();
    }

    private void CloseToolTip()
    {
        if (toolTip != null)
        {
            toolTip.IsOpen = false;
        }
    }

    private void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        timer.Stop();
        Application.Current.Dispatcher.BeginInvoke((Action)CloseToolTip, DispatcherPriority.Send);
    }

}

【问题讨论】:

标签: c# wpf


【解决方案1】:

你可以尝试在控件定义中设置它吗?

   xmlns:behaviors="clr-namespace:MyNameSpace.Behavior;assembly=MyNameSpace"            

<ControlType Text="Something">
<ControlType.Behaviors>
<behaviors:EmailValidatorBehavior />
</ControlType.Behaviors>
</ControlType>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2022-01-06
    • 2011-01-11
    • 2021-06-08
    相关资源
    最近更新 更多