简体   繁体   English

ItemsControl ItemTemplate DataTemplate 触发器未触发

[英]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.我有一个带有DataTemplateItemsControl ,它绑定了PicDuinoModuleV2UiObservableCollection Once the IsTriggered flag is true, I want to change the background of the Border .一旦IsTriggered标志为真,我想更改Border的背景。 Unfortunately, the trigger will not fire.不幸的是,触发器不会触发。 PropertyChanged is also null. PropertyChanged也是 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.您的PicDuinoModuleV2Ui class 没有实现INotifyPropertyChanged接口。 Try to add the interface.尝试添加接口。

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

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

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