Wpf gridview 选定的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16439072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:44:56 来源:igfitidea点击:
Wpf gridview selected Item
提问by Mandar Jogalekar
I have ListViewwith GridViewinside view of ListViewand ListViewitem source is specified. I dont seem to find how can. I get SelectedItemof GridViewor SelectedItemchanged.
我ListView用GridView的内部视图ListView,并ListView指定了项目源。我似乎没有找到怎么办。我得到SelectedItem的GridView或SelectedItem改变。
<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>
回答by Wojciech Kulik
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
}

