【问题标题】:MVVM light Silverlight multiple Selection list box initial selection on page loadMVVM light Silverlight 多选列表框初始选择页面加载
【发布时间】:2012-06-05 19:50:11
【问题描述】:

我正在使用 MVVM 灯,并且有一个可以多选的列表框。在我的 Mainpage.xaml 我有

<ListBox Name="ListBox1" ItemsSource="{Binding Items}" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"  Margin="15,15,18,0" SelectionMode="Multiple" Height="100" />

在 MainPage.xaml.cs 我有(出于某种原因我不想使用依赖属性)。

MainPage()
{
    ListBox1.SelectionChanged = new SelectionChangedEventHandler(ListBox1_SelectionChanged);
}

void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 var listBox = sender as ListBox;
 var viewModel = listBox.DataContext as MainViewModel;
 viewModel.SelectedItems.Clear();
 foreach (string item in listBox.SelectedItems)
     viewModel.SelectedItems.Add(item);
}

这可以正常工作并绑定到我的 MainViewModel。但是当页面加载时,我希望默认选择集合项目的第一项。请告诉我如何实现这个

【问题讨论】:

    标签: c# silverlight list mvvm-light


    【解决方案1】:

    我建议使用 ListBox 的 Loaded 事件,然后绑定到集合中的第一项:

    MainPage()
    {
        ListBox1.Loaded += new RoutedEventHandler( OnListBox1Loaded );
        ListBox1.SelectionChanged += new SelectionChangedEventHandler(ListBox1_SelectionChanged);
    }
    
    private void OnListBox1Loaded( object sender, RoutedEventArgs e )
    {
        // make sure the selection changed event doesn't fire
        // when the selection changes
        ListBox1.SelectionChanged -= MyList_SelectionChanged;
    
        ListBox1.SelectedIndex = 0;
        e.Handled = true;
    
        // re-hook up the selection changed event.
        ListBox1.SelectionChanged += MyList_SelectionChanged;
    }
    

    编辑

    如果您不能使用Loaded 事件,那么您需要在模型中创建另一个属性来保存您要选择的项目,然后将该属性分配给ListBoxSelectedItem 属性.

    public class MyModel : INotifyPropertyChanged
    {
    
      private ObservableCollection<SomeObject> _items;
      public ObservableCollection<SomeObject> Items
      {
        get { return _items; }
        set
        {
            _items = value;
            NotifyPropertyChanged( "Items" );
        }
      }
    
      private SomeObject _selected;
      public SomeObject  Selected
      {
        get { return _selected; }
        set
        {
            _selected = value;
            NotifyPropertyChanged( "Selected" );
        }
      }
    
      public void SomeMethodThatPopulatesItems()
      {
        // create/populate the Items collection
    
        Selected = Items[0];
      }
    
      // Implementation of INotifyPropertyChanged excluded for brevity
    
    }
    

    XAML

    <ListBox ItemsSource="{Binding Path=Items}" 
             SelectedItem="{Binding Path=Selected}"/>
    

    通过拥有另一个保存当前选定项目的属性,您还可以在模型中访问该项目,以及每当用户更改选定项目时。

    【讨论】:

    • 什么是 MyList_SelectionChanged。假设 ListBox1_SelectionChanged 当我尝试在 ListBox1.SelectedIndex = 0;我得到“ArgumentOutOfRange Exception”,因为 ListBox1 的 ItemsSource 尚未加载
    • 该错误是因为尚未发生绑定;请参阅我的编辑以使用更多 MVVM 方法,该方法使用另一个属性来保存 ListBox 中当前选定的项目。
    • 我还有一个小问题要问你。当列表框的内容根据页面上其他一些控件的变化而变化时。如何将所选值保留到第一个元素。
    • 所有答案的左上角是一个向上/向下箭头;如果它有帮助,请点击,如果它是可怕的,请点击。任何您认为对您有帮助的答案,请投票。如果您提出了问题,请单击复选标记以接受对您的问题最有帮助的答案。另外,请参阅FAQ
    • 你现在有 12 个,所以你应该很好。享受吧! (堆栈溢出)
    猜你喜欢
    • 2011-04-15
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2011-01-28
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多