WPF ListView 设置 SelectedItem

标签 wpf listview wpf-controls selecteditem

我试图寻找这个问题的答案,但我没有任何运气。基本上我有一个 ListView ,它绑定(bind)到从 View 模型返回的集合。我将 ListView 的选定项目绑定(bind)到 ListView 中的属性,以便执行验证以确保选择项目。问题是有时我想加载此 ListView 与已选择的项目之一。我希望能够使用我想要选择的对象在我的 View 模型上设置属性,并让它自动选择该项目。这并没有发生。我的 ListView 加载时未选择任何项目。我可以成功地将所选索引设置为第 0 个索引,那么为什么我不能设置所选值。 ListView 处于单选模式。

这是我的 ListView 中的相关代码

<ListView Name="listView1" ItemsSource="{Binding Path=AvailableStyles}" SelectionMode="Single">
            <ListView.SelectedItem>
                <Binding Path="SelectedStyle" ValidatesOnDataErrors="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" BindingGroupName="StyleBinding" >

                </Binding>
            </ListView.SelectedItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="StyleImage">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                               <Image Source="800.jpg"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Style Code" DisplayMemberBinding="{Binding StyleCode}"/>
                    <GridViewColumn Header="Style Name" DisplayMemberBinding="{Binding StyleName}"/>
                </GridView>
            </ListView.View>
        </ListView>

这是我的 View 模型中的相关代码

public class StyleChooserController : BaseController, IDataErrorInfo, INotifyPropertyChanged
{
    private IList<Style> availableStyles;
    private Style selectedStyle;

    public IList<Style> AvailableStyles 
    {
        get { return availableStyles; }
        set
        {
            if (value == availableStyles)
                return;

            availableStyles = value;

            OnPropertyChanged("AvailableStyles");
        }
    }
    public Style SelectedStyle 
    {
        get { return selectedStyle; }
        set
        {
            //if (value == selectedStyle)
            //    return;

            selectedStyle = value;

            OnPropertyChanged("SelectedStyle");
        }
    }

    public StyleChooserController()
    {
        AvailableStyles = StyleService.GetStyleByVenue(1);

        if (ApplicationContext.CurrentStyle != null)
        {
            SelectedStyle = ApplicationContext.CurrentStyle;
        }
    }

    public string Error
    {
        get { return null; }
    }

    public string this[string columnName]
    {
        get
        {
            string error = string.Empty;
            if (columnName == "SelectedStyle")
            {
                if (SelectedStyle == null)
                {
                    error = "required";
                }
            }

            return error;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

}

我应该注意,这里引用的“样式”与 WPF 无关。这是一个商业对象。我确实在寻找一种不会破坏 MVVM 模式的解决方案,但我愿意让某些功能发挥作用。我尝试循环遍历 Listview.Items 列表只是为了手动设置它,但当我尝试时它总是空的。如有任何帮助,我们将不胜感激。

编辑:我更新了代码以使用 INotifyPropertyChanged。它仍然不起作用。任何其他建议 第二次编辑:我添加了 UpdateSourceTrigger="PropertyChanged"。那还是不行。

谢谢

最佳答案

您的问题很可能是因为您的 SelectedItem 引起的Style是不同的Style实例比 AvailableStyles 中的匹配实例要多在 ItemsSource .

您需要做的是在Style中提供您对平等的具体定义。类:

public class Style: IEquatable<Style>
{
    public string StyleCode { get; set; }
    public string StyleName { get; set; }
    public virtual bool Equals(Style other)
    {
        return this.StyleCode == other.StyleCode;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Style);
    }
}

关于WPF ListView 设置 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2291359/

相关文章:

WPF Grid - 自动对齐它的子项

.net - 替换 WPF 入口点

wpf - WPF 中的无边框窗口

c# - WPF ListView 选定项样式

c# - 将 Wpf 的数据网格绑定(bind)到数据库

vb.net - 如何获取wpf按钮的父级

c# - 对象引用未设置为对象的实例 (CacheRequest)(UI)

WPF:绑定(bind)到我的自定义控件不会更新

android - 为 RecyclerView 下载图片时出现性能问题

Android Listview 回收是带动画行的