Wpf网格视图选定项

3

我有一个带有GridViewListView,并且在ListView视图中指定了ListView项目源。我不知道如何获取GridViewSelectedItemSelectedItem更改事件。

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

看起来你在这里拼写错误了:SelectedItem="{Binding Path=selectedItem}" -> SelectedItem="{Binding Path=SelectedItem}" - Peter
1
嗯,我有非常类似的代码,并且它可以正常工作(绑定到ListView的SelectedItem和SelectionChanged会捕获内部GridView)。你能否提供更多出错的细节? - Kevin DiTraglia
我的选择更改事件没有触发。所以它卡在那里了...我想我可以删除那个selectedItem属性,因为它也不起作用。 - Mandar Jogalekar
请您能否发布底层的ViewModel,以便我们能够理解绑定? - Shackles
1个回答

1
这是我的代码,它运行得很好:

Here is my code and it works fine:

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
}

在更新时遇到了类似的问题,结果发现我在构造函数中缺少了这一行代码:DataContext = this; - Califf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接