简体   繁体   中英

ItemsControl ItemTemplate DataTemplate Trigger not firing

I'm almost in despair here and unfortunately can't find the solution. I have an ItemsControl with a DataTemplate which an ObservableCollection of PicDuinoModuleV2Ui are bound. Once the IsTriggered flag is true, I want to change the background of the Border . Unfortunately, the trigger will not fire. PropertyChanged is also null.

<ItemsControl Grid.Row="1" ItemsSource="{Binding ViewModel.Modules}" Margin="10">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border x:Name="Border1" BorderThickness="2" Background="WhiteSmoke" BorderBrush="Black" CornerRadius="5" Width="200" Height="200" Margin="5" Padding="5">
                ...
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsTriggered}" Value="True">
                    <Setter TargetName="Border1" Property="Background" Value="LightGreen"></Setter>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
public class MainWindowViewModel : INotifyPropertyChanged, IDisposable
{
    public ObservableCollection<PicDuinoModuleV2Ui> Modules { get; } = new ObservableCollection<PicDuinoModuleV2Ui>();

    /// <summary>
    /// The PropertyChanged Eventhandler
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raise/invoke the propertyChanged event!
    /// </summary>
    /// <param name="propertyName"></param>
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class PicDuinoModuleV2Ui : PicDuinoModuleV2, IPicDuinoModuleUiProperties
{
    public bool IsTriggered
    {
        get => _isTriggered;
        set
        {
            _isTriggered = value;
            OnPropertyChanged();
        }
    }

    private bool _isTriggered;

    /// <summary>
    /// The PropertyChanged Eventhandler
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raise/invoke the propertyChanged event!
    /// </summary>
    /// <param name="propertyName"></param>
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

在此处输入图像描述

Your PicDuinoModuleV2Ui class does not implement INotifyPropertyChanged interface. Try to add the interface.

public class PicDuinoModuleV2Ui : PicDuinoModuleV2, IPicDuinoModuleUiProperties, INotifyPropertyChanged
{
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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