WPF ListView设置SelectedItem
我试图寻找一个答案,但我没有任何运气。基本上,我有一个绑定到从视图模型返回的集合的listview。我将列表视图的选定项绑定到我的列表视图中的属性,以便执行验证以确保选中某个项。问题是,有时我想加载此listview,其中包含一个已选择的项。我希望能够在我的视图模型上设置我想要选择的对象的属性,并让它自动选择该项目。这并没有发生。“我的listview”加载时未选择任何项目。我可以成功地将所选索引设置为第0个索引,因此为什么我不能设置所选值。列表视图处于单选模式 下面是我列表视图中的相关代码WPF ListView设置SelectedItem,wpf,listview,wpf-controls,selecteditem,Wpf,Listview,Wpf Controls,Selecteditem,我试图寻找一个答案,但我没有任何运气。基本上,我有一个绑定到从视图模型返回的集合的listview。我将列表视图的选定项绑定到我的列表视图中的属性,以便执行验证以确保选中某个项。问题是,有时我想加载此listview,其中包含一个已选择的项。我希望能够在我的视图模型上设置我想要选择的对象的属性,并让它自动选择该项目。这并没有发生。“我的listview”加载时未选择任何项目。我可以成功地将所选索引设置为第0个索引,因此为什么我不能设置所选值。列表视图处于单选模式 下面是我列表视图中的相关代码 &
<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>
下面是我的视图模型中的相关代码
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);
}
}
}
公共类样式选择器控制器:BaseController、IDataErrorInfo、INotifyPropertyChanged
{
私有IList可用样式;
私人风格选择风格;
公共IList可用样式
{
获取{return availableStyles;}
设置
{
如果(值==可用样式)
返回;
可用样式=值;
关于财产变更(“可用样式”);
}
}
公共风格SelectedStyle
{
获取{return selectedStyle;}
设置
{
//如果(值==selectedStyle)
//返回;
selectedStyle=值;
OnPropertyChanged(“SelectedStyle”);
}
}
公共样式选择器控制器()
{
AvailableStyles=StyleService.GetStyleByVincement(1);
if(ApplicationContext.CurrentStyle!=null)
{
SelectedStyle=ApplicationContext.CurrentStyle;
}
}
公共字符串错误
{
获取{return null;}
}
公共字符串此[string columnName]
{
得到
{
字符串错误=string.Empty;
如果(columnName==“SelectedStyle”)
{
如果(SelectedStyle==null)
{
error=“必需”;
}
}
返回误差;
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
var e=新的PropertyChangedEventArgs(propertyName);
处理者(本,e);
}
}
}
我应该注意,这里引用的“样式”与WPF无关。这是一个商业对象。我真的在寻找一个不会打破MVVM模式的解决方案,但我愿意让它发挥作用。我尝试在Listview.Items列表中循环,只是为了手动设置它,但尝试时它总是空的。感谢您的帮助
编辑:我更新了代码以使用INotifyPropertyChanged。它仍然不起作用。还有其他建议吗
第二次编辑:我添加了UpdateSourceTrigger=“PropertyChanged”。这仍然不起作用
谢谢嗯。。。您似乎忘记为
SelectedStyle
属性实现…您的问题很可能是由于您的SelectedItem
Style
与ItemsSource
中的AvailableStyles
中的匹配实例不同而导致的
您需要做的是在样式
类中提供平等的具体定义:
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);
}
}
公共类样式:IEquatable
{
公共字符串样式代码{get;set;}
公共字符串样式名{get;set;}
公共虚拟布尔等于(样式其他)
{
返回this.StyleCode==other.StyleCode;
}
公共覆盖布尔等于(对象对象对象)
{
返回等于(obj作为样式);
}
}
谢谢你指出这一点。我试过了,但还是不起作用。我将自动实现的属性更改为具有显式私有变量,并在set属性中触发事件。仍然不会高亮显示或更改选定的值。这是非常奇怪的,因为当你建议,我认为它肯定会工作。我进行了调试以确保事件被触发。我只是稍微靠近了一点,处理程序为null,因此它没有连接事件。我不太清楚为什么。通过指定绑定,我认为它是自动的。我道歉,因为这是新手,但你的帮助是感激的。太棒了。。。这确实奏效了。有没有更好的方法是我不知道的?这是一个不错的解决方案,但它让我觉得我做错了什么。我创建一个单独属性的主要原因是为了能够验证它是否在视图模型中被选中。再次感谢你的帮助。你的方法很好。像这样实现IEquatable对于模型对象来说是一个很好的实践。唯一的替代方法是确保SelectedItem对象实例包含在ItemsSource集合中(引用相等)。这可能需要采用不同的方法在应用程序中加载对象。