1

我在 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

4

1 回答 1

2

在你的视图中,你会为你的 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”。

于 2012-08-13T19:12:39.457 回答