简体   繁体   English

来自父用户控件的访问绑定变量

[英]Access bound variables that come from parent user control

I have a parent user control which holds another one called TrainDetailsControl . 我有一个父用户控件,其中包含另一个名为TrainDetailsControl The data context of the parent user control is set to a ViewModel class (see below). 父用户控件的数据上下文设置为ViewModel类(请参见下文)。

<UserControl>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox ItemsSource="{Binding Trains}"
                 SelectedItem="{Binding SelectedTrain}"
                 DisplayMemberPath="Name"/>
        <local:TrainDetailsControl Grid.Column="1" DataContext="{Binding SelectedTrain}"/>
    </Grid>
</UserControl>

What I do is "pass" the selected train in the list box to the TrainDetailsControls . 我要做的是将列表框中的选定火车“传递”到TrainDetailsControls Inside that user control I show the details of the selected train object. 在该用户控件内,我显示了所选火车对象的详细信息。

<UserControl>
    <StackPanel>
        <TextBlock Text="{Binding Name}"/>
        <TextBlock Text="{Binding Details}"/>
    </StackPanel>
</UserControl>

This is the ViewModel with the shared variables (this is not the complete code, just to give you an idea). 这是带有共享变量的ViewModel(这不是完整的代码,只是为了给您一个想法)。

public class TrainViewModel : ViewModelBase
{
    public ObservableCollection<Train> Trains { get; }
        = new ObservableCollection<Train>();

    private Train selectedTrain;
    public Train SelectedTrain
    {
        get { return selectedTrain; }
        set { SetValue(ref selectedTrain, value); }
    }
}

This works well, as when I select a train item from the list box, its details are shown correctly in the other user control. 这很好用,因为当我从列表框中选择一个火车项目时,其详细信息会在其他用户控件中正确显示。 But I'm wondering if it is possible to access Name and Details variables from code-behind in TrainDetailsControl . 但是我想知道是否可以从TrainDetailsControl代码隐藏中访问Name和Details变量。 Would the data context of this last user control be the same as the data context of its parent user control (ie the view model class)? 最后一个用户控件的数据上下文是否与其父用户控件(即视图模型类)的数据上下文相同?

But I'm wondering if it is possible to access Name and Details variables from code-behind in TrainDetailsControl 但是我想知道是否可以从TrainDetailsControl的代码隐藏中访问Name和Details变量

Sure. 当然。 Just cast the DataContext to Train once the view has been loaded: 加载视图后,只需将DataContextTrain即可:

public partial class TrainDetailsControl : UserControl
{
    public TrainDetailsControl()
    {
        InitializeComponent();
        Loaded += (s, e) =>
        {
            Train selectedTrain = DataContext as Train;
            if (selectedTrain != null)
            {
                //...
            }
        };
    }
}

Would the data context of this last user control be the same as the data context of its parent user control (ie the view model class)? 最后一个用户控件的数据上下文是否与其父用户控件(即视图模型类)的数据上下文相同?

No, it would the object that SelectedTrain returns since you have bound the DataContext property to this source property. 不,因为您已将DataContext属性绑定到此源属性,所以SelectedTrain将返回该对象。 If you don't explicitly set the DataContext property, the DataContext would be inherited from the parent UserControl though. 如果未显式设置DataContext属性,则DataContext将从父UserControl继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM