【问题标题】:ObservableCollection of a list of strings in WPF MVVMWPF MVVM 中字符串列表的 ObservableCollection
【发布时间】:2012-08-13 18:57:35
【问题描述】:

我在 ObservableCollection 中有一个字符串列表,我想将它绑定到数据网格、列表框或允许我在列表中的项目上添加双击事件的东西。如果你看不出来,我是 WPF 新手!!!

代码:

    private ObservableCollection<string> _searchResults;
    public ObservableCollection<string> SearchResults
    {
        get { return _searchResults; }
        set
        {
            _searchResults = value;
            OnPropertyChanged("SearchResults");
        }
    }

<Grid>
    <DataGrid AutoGenerateColumns="False"
              Name="MyGrid"
              Height="400"
              Width="400"
              ItemsSource="{Binding SearchResults}"
              SelectedItem="{Binding MySelectedItemProperty, UpdateSourceTrigger=PropertyChanged}"/>

</Grid>

顺便说一句,我也在使用 Caliburn.Micro

【问题讨论】:

    标签: wpf list observablecollection


    【解决方案1】:

    在你的视图中,你会为你的 Datagrid 做这样的事情:

    <DataGrid AutoGenerateColumns="False" Name="MyGrid" 
      ItemsSource="{Binding MyListofStrings}"
      SelectedItem="{Binding MySelectedItemProperty, UpdateSourceTrigger=PropertyChanged}"
                  CommandHelper:MouseDoubleClick.Command="{Binding MyEditCommand}">
    

    然后在您的视图模型中:

             private ObservableCollection<MyStrings> _MyListofStrings;
            public ObservableCollection<MyStrings> MyListofStrings
            {
                get { return _MyListofStrings; }
                set
                {
                      _MyListofStrings = value;
                    OnPropertyChanged("MyListofStrings");       //Used for 2 way binding.
                }
            }
    

    然后使用您的数据类型填充“MyListofStrings”。

    【讨论】:

    • 您应该提及CommandHelper:MouseDoubleClick.Command 的来源。它不是标准 WPF 的一部分。
    • 那么你从哪里得到 ?我正在创建私有 ObservableCollection s = new ObservableCollection.
    • 网格也没有被填充。
    猜你喜欢
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2015-03-29
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多