There is a viewmodel.

public class VM_searchResult : DependencyObject, INotifyPropertyChanged { private CCard createdCard; private Commands selectSpravInfo; public Commands SelectSpravInfo { get { return selectSpravInfo ?? (selectSpravInfo = new Commands(obj => { W_selectSprav wSelectSprav = new W_selectSprav(obj as CCard); wSelectSprav.ShowDialog(); })); } } public CCard CreatedCard { get { return createdCard; } set { createdCard = value; OnPropertyChanged("CreatedCard"); } } } 

View

 <StackPanel Orientation="Horizontal" DataContext="{Binding CreatedCard}"> <TextBlock Text="Номер регистрации:" Margin="10,7,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/> <TextBox x:Name="TB_regnum" Text="{Binding Regnum}" Margin="10,7,0,0" VerticalAlignment="Top" MinWidth="90"/> <TextBlock Text="База данных:" Margin="10,7,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/> <Button x:Name="B_selectDB_card" Command="{Binding SelectSpravInfo}" CommandParameter="{Binding CreatedCard}" Content="..." Margin="10,7,0,0" VerticalAlignment="Top" Width="20"/> <TextBox x:Name="TB_db_card" Text="{Binding Bdncpi._name}" Margin="10,7,15,0" TextWrapping="Wrap" VerticalAlignment="Top" IsEnabled="False" MinWidth="200"/> </StackPanel> 

In such a situation, the SelectSpravInfo command is searched in the context of a CreatedCard , which is not there actually. Hence the question, how to get out of this situation? No way, somehow prescribe the context of each textbox.

  • 2
    an element whose DataContext VM_searchResult is given a name, for example root. Then bind to the following: {Binding ElementName = root, Path = DataContext.SelectSpravInfo} - Gardes
  • VM_searchResult has context at the view itself. Those. public MainWindow () {InitializeComponent (); this.DataContext = new VM_searchResult (); } - Rans
  • one
    in this view and give the name: x: Name = "root" - Gardes
  • Shine! Immediately did not understand what you mean, but now figured out. In the end, I did the following DataContext = "{Binding ElementName = root}" Command = "{Binding Path = DataContext.SelectSpravInfo}". Thanks for the help - Rans
  • @ S.Kost: Why not issue a response? - VladD

2 answers 2

We give the name to the element whose DataContext is VM_searchResult (where the command to which you want to bind is located). Next, bind the desired property via ElementName

 <UserControl x:Name="root"> <!--DataContext устанавливается например сверху --> <StackPanel DataContext="{Binding CreatedCard}"> <Button Command="{Binding ElementName=root, Path=DataContext.SelectSpravInfo}" CommandParameter="{Binding ElementName=root, Path=DataContext.CreatedCard}"/> </StackPanel> </UserControl> 

    Decision. Gave the name to the x:Name="root" and then used it as DataContext="{Binding ElementName=root}" Command="{Binding Path=DataContext.SelectSpravInfo}"