0
我創建了一個自定義UserControl。它有一個自定義的依賴屬性。樣式具有使用該屬性的數據觸發器。當表單加載ExpectsFunction.get()被調用兩次。然後我將ExpectsFunction設置爲true。如果我在setter中放置一個斷點,我看到SetValue被正確調用,並且屬性ExpectsFunction變爲'true'。但是,DataTrigger似乎沒有對這種變化做出反應。我是否需要實現一些額外的事件,或實現INotifyPropertyChanged並從DependencyProperty的回調中手動觸發PropertyChanged?我以爲DataTrigger將訂閱的DependencyProperty,但它似乎並沒有這樣的情況:/WPF DataTrigger將不會監視更改
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}" />
...
謝謝!