我是wpf的新手。这是我的oneUserControl,其中有一个数据网格。
<DataGrid ItemsSource="{Binding TraceMessages}"
AutoGenerateColumns="False"
CanUserReorderColumns="True"
CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Severity" Binding="{Binding Path=Severity}"/>
<DataGridTextColumn Header="Context" Width="300" Binding="{Binding Path=Context}"/>
<DataGridTextColumn Header="Correlation Id" Binding="{Binding Path=CorrelationId}"/>
<DataGridTextColumn Header="Message Body" Width="600" Binding="{Binding Path=Data}"/>
</DataGrid.Columns>
</DataGrid>
我还有另一个用户控件,我需要访问哪个视图模型才能从datagrid上访问数据。 ?
答案 0 :(得分:0)
如果视图的ViewModel中有另一个ViewModel,则可以在创建2个VM之后执行此操作。在OnYourFirstVMPropertyChanged
方法更改中设置的任何属性之后,这将使您的第一个VM与第二个VM并存:
public class YourVMDataSource
{
#region Attributes
private readonly YourFirstVM yourFirstVM;
private readonly YourSecondVM yourSecondVM;
#endregion
#region Public Methods
public NumeracionFacturasDisponiblesVMDataSource(YourFirstVM yourFirstVM, YourSecondVM yourSecondVM)
{
this.yourFirstVM = yourFirstVM;
this.yourSecondVM = yourSecondVM;
}
public void Initialize()
{
this.yourFirstVM.PropertyChanged += this.OnYourFirstVMPropertyChanged;
}
#endregion
#region Event Handlers
private void OnYourFirstVMPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
YourFirstVM yourFirstVMSender = (YourFirstVM) sender;
switch (e.PropertyName)
{
case "WhateverPropertyYouWantToSincronize":
this.YourSecondVM.PropertyYouWantToGet = sender. WhateverPropertyYouWantToSincronize;
break;
}
}
#endregion
}
请再次询问是否还没有收到。