简体   繁体   English

WPF-触发器不触发

[英]WPF - Trigger not firing

I'm having an issue when trying to do something which should be as easy as. 尝试执行应尽可能简单的操作时遇到问题。 I've attempted to use a Trigger based on a DependencyProperty or a DataTrigger - I can't get either to work. 我已经尝试使用Trigger根据DependencyPropertyDataTrigger -我不能得到任何工作。

XAML for the trigger is: 触发器的XAML是:

<Style x:Key="FileWatchButton" BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="Main:Main.XmlFilesAvailableForLoading" Value="True">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

And the associated code-behind is: 并且相关的代码背后是:

public static readonly DependencyProperty XmlFilesAvailableForLoadingProperty =
DependencyProperty.Register("XmlFilesAvailableForLoading", typeof(bool), typeof(Main));

public bool XmlFilesAvailableForLoading
{
    get
    {
        try
        {
            return (bool)this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.DataBind,
                    (System.Windows.Threading.DispatcherOperationCallback)delegate { return GetValue(XmlFilesAvailableForLoadingProperty); },
                    XmlFilesAvailableForLoadingProperty);
        }
        catch (Exception)
        {
            return (bool)XmlFilesAvailableForLoadingProperty.DefaultMetadata.DefaultValue;
        }
    }
    set
    {
        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.DataBind,
                (System.Threading.SendOrPostCallback)delegate{ SetValue(XmlFilesAvailableForLoadingProperty, value); }, value);
    }
}

Basically the dp is being set correctly by the presenter (it's based on a FileSystemWatcher class looking for one or more files) but the Trigger is not being fired. 基本上,演示者可以正确设置dp(它基于一个或多个文件的FileSystemWatcher类),但是不会Trigger Is this a threading issue? 这是线程问题吗?

Thanks. 谢谢。

It's not clear if the code is complete, but it looks like the Property path in your trigger may be wrong. 目前尚不清楚代码是否完整,但是看起来触发器中的Property路径可能是错误的。 Does the button being styled have a Main property? 样式化的按钮是否具有Main属性? I am guessing not; 我猜不是; it looks like you are trying to trigger on a property of a different element, called Main -- is that right? 看起来您正在尝试触发另一个名为Main的元素的属性,对吗?

In any case, the namespace prefix is not required. 无论如何,都不需要名称空间前缀。 If the button has a property named Main, then you can address this directly; 如果按钮具有名为Main的属性,则可以直接解决此问题; if it doesn't, then the prefix won't help you. 如果没有,那么前缀将无济于事。

My guess is that you probably need a DataTrigger whose binding refers to the Main element: 我的猜测是,您可能需要一个DataTrigger,其绑定引用Main元素:

<local:Main Name="MyMain" ... />  <!-- this has the XmlFilesAvailableForLoading property -->

<DataTrigger Binding="{Binding XmlFilesAvailableForLoading, ElementName=MyMain}"
             Value=True>
  <Setter Property="Background" Value="Red" />
</DataTrigger>

On an unrelated note, you should have any non-boilerplate implementation in your DP getter and setter. 无关紧要的是,您在DP getter和setter中应该有任何非样板实现。 Remember that the binding and styling system will bypass the getter and setter and talk directly to the underlying storage. 请记住,绑定和样式系统将绕过getter和setter并直接与基础存储进行对话。 So I'd strongly advise changing these back to just plain GetValue and SetValue calls. 因此,我强烈建议将其改回为普通的GetValue和SetValue调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM