Why in Xaml do not work tips with the names of the properties of VM-ki? But if you enter the correct name of the property, then everything compiles and works well.

I bind VM to the window like this:

public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new MainWindow() { DataContext = new MainVm() }.Show(); } } public class MainVm : VmBase { private string _input; public string Input { get { return _input; } set { _input = value; OnPropertyChanged(); } } } <Window x:Class="WarehouseClient2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MainWindow" Width="525" Height="350" mc:Ignorable="d"> <Grid> <TextBox Text="{Binding Input}"></TextBlock> </Grid> 

  • Give more code. This code is correct. - VladD
  • @VladD, Added the code. The Editor does not see the Input property at all, but if you enter it yourself, then everything works. - Lightness
  • And at runtime, see? - VladD
  • @VladD, yes, sees at runtime. - Lightness
  • 2
    You simply forgot to declare the expected type of context. For example - stackoverflow.com/questions/29394295 - Monk

2 answers 2

In WPF, XAML views can be reused. Therefore, if you write a form for a specific VM, you can specify its type so that intellisense tells you the names of properties. A simple example on the knee in a couple of minutes, VM in one property:

  public class ViewModel { public string IamProperty { get; set; } } 

And control under it:

 <UserControl ... xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:LocalizationExample" d:DataContext="{d:DesignInstance local:ViewModel, IsDesignTimeCreatable=True}"> <TextBlock Text="{Binding IamProperty}"/> </UserControl> 

Intellisense tells you which property to substitute:

Example

Wrong problem solving

in fact, you can still do this:

 <UserControl.DataContext> <local:ViewModel/> </UserControl.DataContext> <TextBlock Text="{Binding IamProperty}"/> 

intellisense will work. Only here the DataContext is created in the markup and then a lot of unpleasant problems can occur if no one is waiting for it. Saw more than once in the working code, therefore I finish separately.

    Your code is correct.

    The problem is this: XAML, unlike C #, is a weakly typed language. Therefore, at the editing stage, it is not yet known what real type will fall into the DataContext , whether it will have the necessary properties. For example, you can write

     if (DateTime.Now.DayOfWeek == DayOfWeek.Friday) window.DataContext = new MainVm(); else window.DataContext = "привет";