如何绑定窗口加载的事件?

时间:2017-09-30 18:58:27

标签: wpf data-binding

我想从我的视图中绑定已加载的事件,以便我可以在开始时阅读一些设置。通过一些搜索,我做了这个,但它不起作用。我错过了什么?

观点:

<UserControl x:Name="UserControlRegistratie" x:Class="Qbox_0001.Views.RegistratieView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             mc:Ignorable="d" HorizontalContentAlignment="Left" VerticalContentAlignment="Top"

             xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
>
    <intr:Interaction.Triggers>
        <intr:EventTrigger EventName="Loaded">
            <intr:InvokeCommandAction Command="{Binding Path=windowLoadedCommand}"/>
        </intr:EventTrigger>
    </intr:Interaction.Triggers>

    <Grid x:Name="GridRegistratie">
    .....

viewmodel:

public class RegistratieViewModel
    {
        public RelayCommand windowLoadedCommand { get; private set; }

        public RegistratieViewModel()
        {
            ...
            //commands
            windowLoadedCommand = new RelayCommand(ExecuteWindowLoaded, CanExecute);
        }

        private bool CanExecute(object parameter)
        {
            return true;
        }

        private void ExecuteWindowLoaded(object parameter)
        {
            MessageBox.Show("window laden...........");
            //Nothing happens
        }
    }

1 个答案:

答案 0 :(得分:0)

在我看来,我有这个:(适用于其他绑定)

public partial class RegistratieView : UserControl
    {
        public RegistratieView()
        {
            InitializeComponent();
            DataContext = new RegistratieViewModel();
        }
    }