Wpf Usercontrol, Check if RoutedEvent is bound

150 Views Asked by At

I have the following UserControl:

<UserControl x:Class="DueVCheck.Pl.Gui.CustomControl.ListInfoBar"
         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"
         xmlns:local="clr-namespace:DueVCheck.Pl.Gui.CustomControl"
         mc:Ignorable="d" d:DataContext="{d:DesignInstance local:ListInfoBar}" >      
    <Button Name="NewRowBtn" Margin="5" Click="NewRowBtn_OnClick" Content="New"/>
</UserControl>   

The Code-Behind file:

using System.Windows;

namespace DueVCheck.Pl.Gui.CustomControl
{
    public partial class ListInfoBar
    {       
        public ListInfoBar()
        {
            DataContext = this;
            InitializeComponent();
        }

        public static readonly RoutedEvent NewClickEvent =
        EventManager.RegisterRoutedEvent("NewClick", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(ListInfoBar));

        public event RoutedEventHandler NewClick
        {
            add { AddHandler(NewClickEvent, value); }
            remove { RemoveHandler(NewClickEvent, value); }
        }

        private void NewRowBtn_OnClick(object sender, RoutedEventArgs e) 
        { 
            RaiseEvent(new RoutedEventArgs(NewClickEvent)); }
        }
    }
}

Usage of the Usercontrol:

<customControl:ListInfoBar Margin="10,0,10,0" NewClick="NewRowOnClick"/>

What i need is that the Usercontrol disables the NewRowBtn whenever the NewClick is not bound via XAML. The Problem is that Add or Remove get never called when binding the Event over XAML. How to fix this?

0

There are 0 best solutions below