簡體   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