2013-05-08 64 views
3

我有ListViewGridView內部視圖ListViewListView項目來源被指定。我似乎沒有找到如何可以。我得到SelectedItemGridViewSelectedItem已更改。Wpf gridview選項

<ListView Grid.Row="4" Margin="0,250,0,0" ItemsSource="{Binding TestBinding}" SelectedItem="{Binding Path=selectedItem}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" SelectionChanged="ListView_SelectionChanged"> 
    <ListView.View> 
     <GridView AllowsColumnReorder="False" > 
      <GridViewColumn Header="Test" DisplayMemberBinding="{Binding Path=Test1}" Width="100" /> 
      <GridViewColumn Header="Test2" DisplayMemberBinding="{Binding Path=Test2}" Width="130" />     
     </GridView> 
    </ListView.View> 
</ListView> 
+0

看來你有拼寫錯誤的位置:的SelectedItem = 「{綁定路徑=將selectedItem}」 - >的SelectedItem =「{綁定路徑= SelectedItem}「 – Peter 2013-05-08 11:11:21

+1

嗯,我有非常類似的代碼給你,它工作正常(綁定到SelectedItem和ListView的SelectionChanged拿起內部的GridView)。你能給出更多的細節問題嗎? – 2013-05-08 11:13:53

+0

我選擇的變化事件不會引發up.so它卡在那裏..我想我可以刪除selectedItem屬性,因爲它不工作 – 2013-05-08 11:24:54

回答

1

這裏是我的代碼,它工作正常:

public partial class MainWindow : Window, INotifyPropertyChanged, INotifyPropertyChanging 
{ 
    public class MyObj 
    { 
     public string Test1 { get; set; } 
     public string Test2 { get; set; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     TestBinding = new ObservableCollection<MyObj>(); 
     for (int i = 0; i < 5; i++) 
     { 
      TestBinding.Add(new MyObj() { Test1 = "sdasads", Test2 = "sdsasa" }); 
     } 

     DataContext = this; 
    } 

    #region TestBinding 

    private ObservableCollection<MyObj> _testBinding; 

    public ObservableCollection<MyObj> TestBinding 
    { 
     get 
     { 
      return _testBinding; 
     } 
     set 
     { 
      if (_testBinding != value) 
      { 
       NotifyPropertyChanging("TestBinding"); 
       _testBinding = value; 
       NotifyPropertyChanged("TestBinding"); 
      } 
     } 
    } 
    #endregion 

    #region selectedItem 

    private MyObj _selectedItem; 

    public MyObj selectedItem 
    { 
     get 
     { 
      return _selectedItem; 
     } 
     set 
     { 
      if (_selectedItem != value) 
      { 
       NotifyPropertyChanging("selectedItem"); 
       _selectedItem = value; 
       NotifyPropertyChanged("selectedItem"); 
      } 
     } 
    } 
    #endregion 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    // Used to notify the page that a data context property changed 
    protected void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 

    #region INotifyPropertyChanging Members 

    public event PropertyChangingEventHandler PropertyChanging; 

    // Used to notify the data context that a data context property is about to change 
    protected void NotifyPropertyChanging(string propertyName) 
    { 
     if (PropertyChanging != null) 
     { 
      PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
     } 
    } 

    #endregion 
} 
+0

更新類似的問題,原來我在構造函數中缺少這行:DataContext = this; – Califf 2014-03-19 17:56:49