2017-08-08 167 views
1

我是MVVM/WPF的新手,經過幾個小時的研究,沒有找到任何真正有用的工作答案,我決定給它一個去,嘗試在這裏問。WPF使用SelectedIndex從ViewModel中選擇列表框中的項目

我想從列表框中選擇一個項目,它使用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; 
      } 
     } 
    } 
} 

列表框默認包含的幾個項目,最後一個總是被類型Add,而如果選擇,可以添加一個新的項目,在默認情況下選中或選擇之一如果沒有添加新項目,則先前選擇的項目。 For Simplicity無論是否添加新項目,所選項目都將爲1。

無論身在何處,什麼我試圖與OnPropertyChanged更新它並沒有在視圖中更新的SelectedIndex,但通過添加/插入新FavoriteDataVmObservableCollection<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?

回答

3

如何獲取更新ListBox的SelectedIndex?

您可以通過獲取SelectedItem指數的源集合中獲取所選擇的指標:

SelectedItem = Favorites[1]; //selects the second item 

int selectedIndex = (SelectedItem != null && Favorites != null) ? Favorites.IndexOf(SelectedItem) : -1; 

而且你可以通過設置SelectedItem屬性中選擇一個項目您不應該綁定ListBoxSelectedItemSelectedIndex屬性。這些必須同步。從您的XAML中刪除SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"

還要注意的是,如果你打算動態更新源屬性,則需要提高PropertyChanged此屬性的視圖刷新:

private int _selectedIndex; 
public int SelectedIndex 
{ 
    get { return _selectedIndex; } 
    set { _selectedIndex = value; OnPropertyChanged("SelectedIndex"); } 
} 

private FavoriteDataVm _selectedItem; 
public FavoriteDataVm SelectedItem 
{ 
    set 
    { 
     _selectedItem = value; 
     OnPropertyChanged("SelectedItem"); 
    } 
} 
0

您還沒有更新的SelectedIndex 聲明應該如下

 private int _selectedIndex ; 
    public int SelectedIndex{ 
    get{return _selectedIndex }; 
    set{_selectedIndex=value;OnPropertyChanged("SelectedIndex") }; 
    } 

這樣當selectedIndex改變

名單將被告知「對不起,如果有任何錯別字,我直接在這裏寫了這個,而不是在代碼編輯器中,所以你可能會得到錯別字「

+0

感謝百忙之中閱讀我的問題的時候了!我沒有嘗試把事件放在那裏,所以它可能證明是可行的(最可能)。稍後當我回家並根據需要更新此線程時,我會試一試 – Rikidere