How to override DataMontext ViewModel so that I can bind to objects in a view (Mvvm-Light)?

I use Mvvm-Light and ignore how the XAML binding really works so far.

Here is my xaml

<phone:PhoneApplicationPage.Resources>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,14">
        <TextBlock x:Name="ApplicationTitle" Text="{Binding SecuritySystemName}" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="{Binding PageName}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <TextBlock Name="textCode" 
        DataContext="{WHAT GOES HERE to bind to properties on my View (SecurityPanelPage class)}"    
        Text="{Binding Path=Code}" />

</Grid>

{Binding SecuritySystemName} and {Binding PageName} are correctly bound to my ViewModel (SecuirtyPanelViewModel). But I want to {Binding Code} in the TextBlock element to be attached to my VIEW (and not ViewModel).

I searched and searched for documentation and samples that explain the syntax and values ​​supported by DataContext and Binding. Nothing helped.

, , DataContext ( - {Binding...}, View. "" "RelativeSource", . , XAML .

.

- , , , .

:

<phone:PhoneApplicationPage x:Name="ThisPage">
   <TextBlock Name="textCode" Text="{Binding Code, ElementName=ThisPage"/>
</phone:PhoneApplicationPage>

: http://bursjootech.blogspot.com/2007/12/bind-from-xaml-to-local-property-in.html

-: " XAML ".

, . , ...

+3
2

, MVVM, ViewModel, . , , , . , , ?

... , SecurityPanelPage - XAML?

, ElementName. DataContext.

<phone:PhoneApplicationPage
  x:Name="controlName"
  x:Class="SomeNamespace.SecurityPanelPage">
</phone:PhoneApplicationPage>

:

<TextBlock
  Name="textCode" 
  Text="{Binding Path=Code, ElementName=controlName}" />

DataContext, :

<TextBlock
  Name="textCode" 
  DataContext="{Binding ElementName=controlName}"
  Text="{Binding Path=Code}" />
+5

, app.xaml .

A.E:

xmlns:crap="clr-namespace:Application.Views"
<Application.Resources>
    <ResourceDictionary>
        <crap:MyViewPage x:Key="MyViewPage" />
    </ResourceDictionary>  
</Application.Resources>

"{Binding MyProperty, source={StaticResource MyViewPage}".

, /

+1

Source: https://habr.com/ru/post/1786092/


All Articles