C# 带有图像和数据触发器的ListView don';t显示
我不想在wpf里做这样的东西 所以我尝试了这个xaml:C# 带有图像和数据触发器的ListView don';t显示,c#,wpf,C#,Wpf,我不想在wpf里做这样的东西 所以我尝试了这个xaml: <StackPanel HorizontalAlignment="Left"> <ListView ItemsSource="{Binding Path=StateItems}"> <ListView.ItemTemplate> <DataTemplate DataType="vm:StateItem">
<StackPanel HorizontalAlignment="Left">
<ListView ItemsSource="{Binding Path=StateItems}">
<ListView.ItemTemplate>
<DataTemplate DataType="vm:StateItem">
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Active}" Value="True">
<Setter Property="Image.Source" Value="{StaticResource Ellipse}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter Property="Image.Source" Value="{StaticResource EllipseEmpty}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
问题是items[m_InstallationSteps.ActiveStepIndex].Active=true代码>
它为null,但wpf未引发异常。是否已使用ViewModel设置DataContext?加载UI时,StateItems是否已包含项?否则,您必须通知UI有关更改的信息。不确定如何设置?是否尝试在触发器之前添加类似的内容以放置默认值?另外,也许您应该尝试在活动属性的setter中放入NotifyPropertyChanged类调用。我尝试添加,但效果不错。是的,我认为这是因为活动属性setter中缺少NotifyPropertyChanged。或者,如果ViewModel是DependencyObject,则将其设置为Dependency属性。
public List<StateItem> StateItems
{
get
{
var items = new StateItem[m_InstallationSteps.Steps.Count];
items[m_InstallationSteps.ActiveStepIndex].Active = true;
return items.ToList();
}
}
public class StateItem
{
public bool Active { get; set; }
}
public class StateItem : INotifyPropertyChanged
{
private bool m_Active;
public bool Active
{
get { return m_Active; }
set
{
m_Active = value;
OnPropertyChanged("Active");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}