我创建了一个自定义用户控件。它有一个自定义的依赖属性。样式具有使用该属性的数据触发器。当表单加载时 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}" />
...
谢谢!