开发者

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.

XAML for the trigger is:

<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" /开发者_StackOverflow社区>
        </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. 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. Does the button being styled have a Main property? I am guessing not; it looks like you are trying to trigger on a property of a different element, called Main -- is that right?

In any case, the namespace prefix is not required. If the button has a property named Main, then you can address this directly; 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:

<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. Remember that the binding and styling system will bypass the getter and setter and talk directly to the underlying storage. So I'd strongly advise changing these back to just plain GetValue and SetValue calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜