Binding objects defined in code-behind

前端 未结 11 1292
难免孤独
难免孤独 2020-11-28 04:29

I have some object that is instantiated in code behind, for instance, the XAML is called window.xaml and within the window.xaml.cs

protected Dictionary

        
11条回答
  •  暖寄归人
    2020-11-28 04:44

    That's my way to bind to code behind (see property DataTemplateSelector)

    public partial class MainWindow : Window
    {
      public MainWindow()
      {
        this.DataTemplateSelector = new MyDataTemplateSelector();
    
        InitializeComponent();
    
        // ... more initializations ...
      }
    
      public DataTemplateSelector DataTemplateSelector { get; }
    
      // ... more code stuff ...
    }
    

    In XAML will referenced by RelativeSource via Ancestors up to containing Window, so I'm at my Window class and use the property via Path declaration:

    
    
    

    Setting of property DataTemplateSelector before call InitializeComponent depends on missing implementation of IPropertyChanged or use of implementation with DependencyProperty so no communication run on change of property DataTemplateSelector.

提交回复
热议问题