繁体   English   中英

WPF DataTrigger不会监视更改

[英]WPF DataTrigger won't monitor changes

我创建了一个自定义UserControl。 它具有自定义的Dependency属性。 样式具有使用该属性的数据触发器。 加载表单时,ExpectsFunction.get()被调用两次。 然后,我在外部将ExpectsFunction设置为true。 如果在设置器中放置一个断点,则会看到SetValue被正确调用,并且属性ExpectsFunction变为'true'。 但是,DataTrigger似乎对更改没有反应。 我是否需要实现一些其他事件,或者实现INotifyPropertyChanged并从DependencyProperty的回调中手动触发PropertyChanged? 我以为DataTrigger会订阅DependencyProperty,但事实并非如此:/

public partial class MatlabScriptSettings : UserControl
{
    public static readonly DependencyProperty ExpectsFunctionProperty =
        DependencyProperty.Register("ExpectsFunctionProperty", typeof(bool), typeof(MatlabScriptSettings), new PropertyMetadata(false));

    public bool ExpectsFunction
    {
        get
        {
            return (bool)GetValue(ExpectsFunctionProperty);
        }
        set
        {
            SetValue(ExpectsFunctionProperty, value);
        }
    }
}

<UserControl x:Class="PlatformManager.UI.Configuration.MatlabScriptSettings"
             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="400" d:DesignWidth="350"
             x:Name="_this">

    <UserControl.Resources>
        <Style x:Key="ArgumentsStyle" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=_this, Path=ExpectsFunction}" Value="True">
                    <Setter Property="IsReadOnly" Value="False" />
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=_this, Path=ExpectsFunction}" Value="False">
                    <Setter Property="IsReadOnly" Value="True" />
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

...

<TextBox Name="ScriptArguments" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="5,5,0,0" Text="{Binding Path=ScriptArguments}" Style="{StaticResource ResourceKey=ArgumentsStyle}" />

...

谢谢!

容易(浪费4小时后:/)...属性名称的字符串不能包含Property

public static readonly DependencyProperty ExpectsFunctionProperty =
    DependencyProperty.Register("ExpectsFunction", typeof(bool), typeof(MatlabScriptSettings), new PropertyMetadata(false));

暂无
暂无

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

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