如何在使用代理时从ComboBox获取SelectedItem? [英] How to get the SelectedItem from a ComboBox when using a Proxy?
问题描述
嗨!
我正在使用Entity Framework和MVVM Light模式构建DataGrid,因此我将Datagrid绑定到像这样的ListCollectionView:
< DataGrid Grid.ColumnSpan =" 2" ItemsSource =" {Binding RequestsModelView,UpdateSourceTrigger = PropertyChanged,Mode = OneWay}"
SelectedItem =" {Binding SelectedRequest,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}">
ViewModel:
public ListCollectionView RequestsModelView {get;组; }
public Requests RequestsModel
{
get => RequestsModelView?.CurrentItem as Requests;
设置
{
RequestsModelView?.MoveCurrentTo(value);
RaisePropertyChanged();
}
}
private void InitializeRequestsView()
{
RequestsModelView = CollectionViewSource.GetDefaultView(RequestsCollection)作为ListCollectionView;
RequestsModelView.CurrentChanged + =(s,e)=>
{
RaisePropertyChanged(()=> RequestsModel);
};
}
当我使用来自此Requests-ListColletionView的属性(即来自Requests-table Entity的值)时,这非常有效。
现在我想添加一个从另一个表中获取其值的DataGrid ComboBox(即Requests_Functions-table Entity)。我设法使用代理(取自&span; Thomaslevesque.com/2011/03/21/wpf-how-to -bind-to-data-when-datacontext-not-inherited / )。
这是XAML代码:
< DataGridTemplateColumn
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text =" {Binding Requests_Functions.Name,UpdateSourceTrigger = PropertyChanged}" />
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< ComboBox ItemsSource =" {Binding Path = Data.FunctionNamesCollection,Source = {StaticResource proxy},UpdateSourceTrigger = PropertyChanged}"
SelectedItem =" {Binding Requests_Functions.Name,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}"
/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
我现在的问题是,我不知道如何获取SelectedItem(Requests_Functions.Name)正确地在ViewModel中,因为它是一个相关的表,而不是直接来自< ListCollectionView-Entity。
有人知道如何获取SelectedItem吗?我尝试使用RowEditEnding EventTrigger,但总是遇到一个问题,即SelectedItem与< ListCollectionView-Entity不是同一个实体。
谢谢!
嗨 HansiM007,
>>我现在的问题是,我不知道我不知道如何在ViewModel中正确获取SelectedItem(Requests_Functions.Name),因为它是一个相关的表而不是直接来自ListCollectionView-Entity。
你的ComboBox ItemsSource和SelectedItem绑定属性来自不同的viewmodel?
我建议你可以定义一个SelectedItem属性StaticResource代理。
$
或者,您可以尝试在主视图模型中定义SelectedSomething属性,并使用主Windows Datacontext
SelectedItem =" {Binding DataContext.SelectedSomething,RelativeSource = {RelativeSource AncestorType = Windows}}"
如果我有任何误解,你可以上传一个最小的可运行演示到OneDrive(包括你的测试材料和remo)所有私人信息)。我们可以下载并调试(重现您的问题)。这有助于我们快速分析您的问题。
$
最好的问候
Yong Lu
Hi!
I'm building a DataGrid with Entity Framework and MVVM Light pattern and thus I bind the Datagrid to a ListCollectionView like so:
<DataGrid Grid.ColumnSpan="2" ItemsSource="{Binding RequestsModelView, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" SelectedItem="{Binding SelectedRequest, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
ViewModel:
public ListCollectionView RequestsModelView { get; set; } public Requests RequestsModel { get => RequestsModelView?.CurrentItem as Requests; set { RequestsModelView?.MoveCurrentTo(value); RaisePropertyChanged(); } } private void InitializeRequestsView() { RequestsModelView = CollectionViewSource.GetDefaultView(RequestsCollection) as ListCollectionView; RequestsModelView.CurrentChanged += (s, e) => { RaisePropertyChanged(() => RequestsModel); }; }
This works perfect when I use properties from this Requests-ListColletionView (i.e. values from Requests-table Entity).
Now I want to add a DataGrid ComboBox that gets its values from another table (i.e. Requests_Functions-table Entity). I managed to get it working with a proxy (taken from Thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/).
Here is the XAML-code:
<DataGridTemplateColumn <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Requests_Functions.Name, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Path=Data.FunctionNamesCollection, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Requests_Functions.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn>
My problem now is, that I don't know how to get the SelectedItem (Requests_Functions.Name) properly in the ViewModel, because it is a related table and not directly coming from the ListCollectionView-Entity.
Does someone know a way how to get the SelectedItem? I tried with RowEditEnding EventTrigger but always stucking with the problem that the SelectedItem is not the same Entity as the ListCollectionView-Entity.
Thanks!
Hi HansiM007,
>>My problem now is, that I don't know how to get the SelectedItem (Requests_Functions.Name) properly in the ViewModel, because it is a related table and not directly coming from the ListCollectionView-Entity.
Your ComboBox ItemsSource and SelectedItem binding property are coming from different viewmodel?
I suggest you can define a SelectedItem property in StaticResource proxy.
Or, you can try to define a SelectedSomething property in your main view model, and use the Main Windows Datacontext
SelectedItem="{Binding DataContext.SelectedSomething, RelativeSource={RelativeSource AncestorType=Windows}}"
If I have any misunderstanding, you can upload a minimal runnable demo to OneDrive(Including your test material and remove all private information). We can download it and debugging(reproduce your issue). This will help us quickly analyze your problem.
Best Regards
Yong Lu
这篇关于如何在使用代理时从ComboBox获取SelectedItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!