C#
DataContext="{Binding ProveedoresViewModel, Mode=OneWay, Source={StaticResource LocatorViewModel}}"
XAML
<DataGrid Margin="10"
ItemsSource="{Binding ListaProveedores, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
GridLinesVisibility="All"
SelectedItem="{Binding ProveedorSeleccionado, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
CanUserAddRows="False"
BorderThickness="1"
BorderBrush="Black">
ListaProveedores
是ViewModel1,ProveedorSeleccionado
是ViewModel2。
我需要为ProveedorSeleccionado
答案 0 :(得分:0)
如果LocatorViewModel资源通过公共属性公开两个视图模型(ProveedoresViewModel和“ViewModel2”或其他任何一个),您可以将DataGrid的SelectedItem属性绑定到“ViewModel2”的子属性:
<DataGrid Margin="10" ItemsSource="{Binding ListaProveedores, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" GridLinesVisibility="All"
SelectedItem="{Binding ViewModel2.ProveedorSeleccionado, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" CanUserAddRows="False" BorderThickness="1" BorderBrush="Black">
您还可以明确地将绑定的源设置为另一个资源,例如:
SelectedItem="{Binding ProveedorSeleccionado, Source={StaticResource ViewModel2}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"