不显示带有图像和数据触发器的ListView

时间:2013-08-09 11:12:08

标签: c# wpf

我不想在wpf enter image description here

中制作类似的内容

所以我试试这个xaml:

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

这在viewModel:

  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; }
}

但它没有出现。如何让它出现?

编辑: 在我看来,绑定不起作用

EDIT2:尝试使用通知进行更改,但它没有帮助

  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));
    }
}

1 个答案:

答案 0 :(得分:0)

我很傻))问题是items[m_InstallationSteps.ActiveStepIndex].Active = true;

它是null但是wpf没有抛出异常。