WPF主从输入 [英] WPF master-detail input
本文介绍了WPF主从输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个主从用户界面. (可选)用户应该能够使用默认值填充某些字段.
所以我说
I have a master-detail user interface. Optionally, the user should be able to populate some of the fields with default values.
So I say
void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "6545" // this is fine
listView1.Items.Add(new { name = "test" , value = "fifty" }); // this throws an exception
}
例外是:
使用ItemsSource时操作无效.改为使用ItemsControl.ItemsSource访问和修改元素.
??????为什么我不能通过GUI模拟数据输入的代码?
===========
后面的代码的相关部分是:
Exception is:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
????? Why can''t I mimic in code the input of data via the GUI?
============
Relevant portions of the code-behind are:
private void Window_Loaded(object sender, RoutedEventArgs e) {
this.PostData = db.Posts;
MasterViewSource = (CollectionViewSource)this.FindResource("MasterView");
DetailViewSource = (CollectionViewSource)this.FindResource("DetailView");
MasterView = (BindingListCollectionView)this.MasterViewSource.View;
MasterView.CurrentChanged += new EventHandler(MasterView_CurrentChanged);
DetailView = (BindingListCollectionView)this.DetailViewSource.View;
//.................///
}
----------
----------
My xaml has
<CollectionViewSource x:Key="MasterView" />
<CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path=''PostDetails''}" x:Key="DetailView" />
<!-- .................... -->
<ListView Name="listView1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource DetailView}}">
<!-- ............. -->
<ListView.View>
<GridView>
<!-- .............. -->
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource NameLookup}}"SelectedValue="{Binding Path=AccountName}" DisplayMemberPath="name" SelectedValuePath="name" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
-------
谢谢
-------
Thanks
推荐答案
您无法执行此操作,因为您实际上是在使用数据绑定,而不是从头开始直接分配值.您在这里有两个选择,或者将项目添加到您绑定到的集合中(应该实现INotifyCollectionChanged-ObservableCollection在这里是一个不错的选择),或者您需要使用以下内容的变体:
You can''t do this because you are actually using a data binding, rather than directly assigning values from the start. You have two choices here, either add the items to the collection you have bound to (which should implement INotifyCollectionChanged - ObservableCollection is a good choice here), or you need to use a variation of the following:
ICollectionView view = (ICollectionView)CollectionViewSource.GetDefaultView(listView1.ItemsSource);
var item = view.CurrentItem;
item.Add(....);
您去了,应该会有所帮助.
There you go, that should help.
这篇关于WPF主从输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文