【发布时间】:2018-01-15 19:00:53
【问题描述】:
我是 MVVM/WPF 的新手,经过几个小时的研究,没有为我的项目找到任何真正有用/有效的答案,我决定试一试并尝试在这里提问。
我想从我的 Listbox 中选择一个 Item,它使用 List 作为 ItemSource。
相关视图模型:
public class FavoriteStructureVm : INotifyPropertyChanged
{
#region
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
public ObservableCollection<FavoriteDataVm> Favorites { get; set; }
public int SelectedIndex { get; set; }
private FavoriteDataVm _selectedItem;
public FavoriteDataVm SelectedItem
{
set
{
_selectedItem = value;
var item = (FavoriteDataVm)_selectedItem;
if (item.Type == FavoriteDataType.Add)
{
SelectedIndex = 1;
}
}
}
}
ListBox默认包含几个项目,最后一个总是Add类型之一,如果被选中,可以添加一个新项目并默认选择它,或者如果没有新项目则选择之前选择的项目添加。 为了简单,无论是否添加新项目,所选项目都将为 1。
无论我尝试使用OnPropertyChanged 更新的位置和内容,它都不会更新视图中的SelectedIndex,但是,通过将新的FavoriteDataVm 添加/插入到ObservableCollection<FavoriteDataVm> Favorites 中,视图中的SelectedIndex得到更新。
向列表中添加新项目的过程并不总是发生,但我想总是更新SelectedIndex。
相关 XAML:
<ListBox Name="favMenu" ItemsSource="{Binding Favorites}" SelectionMode="Single"
HorizontalAlignment="Center" VerticalAlignment="Top"
BorderThickness="0" Background="Transparent" Height="{Binding ElementName=window, Path=ActualHeight}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Background="Transparent" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<!--changing default orientation-->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="Border"
BorderThickness="0" BorderBrush="Black" Background="{x:Null}"
Width="60" Height="60" CornerRadius="30" Margin="{Binding Margin}"
ToolTip="{Binding Name}">
<Image Source="{Binding ImageUri}" Width="60" Height="60" Stretch="UniformToFill">
<Image.Clip>
<EllipseGeometry RadiusX="30" RadiusY="30" Center="30,30"/>
</Image.Clip>
</Image>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我找到了一种解决方法来创建一个虚拟项目并将其删除,因为添加一些东西似乎会更新视图中的SelectedIndex。我不认为它是一种解决方案,因为它有很多缺点。
所以这实际上提出了两个问题:
- 如何更新列表框的
SelectedIndex?
还有一个初学者问题,因为我是 MVVM 新手:
- 这是 MVVM 的正确实现吗?
【问题讨论】: