[英]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.我有一个带有
DataTemplate
的ItemsControl
,它绑定了PicDuinoModuleV2Ui
的ObservableCollection
。 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.