in

Utah .NET User Group

Home of Utah's professional .NET developers.

Joe's Blog

  • Building a Databound WPF Menu Using a HierarchicalDataTemplate

    I had a colleague ask me how he could solve a databinding problem while building a WPF Menu.  The immediate answer wasn’t apparent to me, though it’s actually quite simple.  A while back Karl Shifflett wrote up an excellent example of doing something similar.  I take his example one step further by showing how you can easily bind  properties to the MenuItem instead of assigning one-time values.

    Download Source:  WPFDataBoundMenu.zip

    We’re going to use a simple Business Object which represents our MenuItem.  It has four properties: MenuText, IsEnabled, Command, and Children.

    public class MyBusinessObject : INotifyPropertyChanged
    {
        private bool _isEnabled;
        private string _menuText;
        private ICommand _command;
        private ObservableCollection<MyBusinessObject> _children;
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        public MyBusinessObject()
        {
            Children = new ObservableCollection<MyBusinessObject>();
        }
     
        public string MenuText
        {
            get { return _menuText; }
            set
            {
                _menuText = value;
                RaisePropertyChanged("MenuText");
            }
        }
     
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                RaisePropertyChanged("IsEnabled");
            }
        }
     
        public ICommand Command
        {
            get { return _command; }
            set
            {
                _command = value;
                RaisePropertyChanged("Command");
            }
        }
     
        public ObservableCollection<MyBusinessObject> Children
        {
            get { return _children; }
            set
            {
                _children = value;
            }
        }
     
        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
     
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    We’ll build our window to include a Menu and  a Button.  When we click the button we want to toggle the IsEnabled property of one of our Business Objects.

    image 

    <Window
        x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1"
        Height="400"
        Width="700">
     
        <Window.Resources>
     
            <HierarchicalDataTemplate
                DataType="{x:Type local:MyBusinessObject}"
                ItemsSource="{Binding Path=Children}">
     
                <ContentPresenter
                    Content="{Binding MenuText}"
                    Loaded="ContentPresenter_Loaded"
                    RecognizesAccessKey="True" />
     
            </HierarchicalDataTemplate>
     
        </Window.Resources>
     
        <Grid
            Margin="10">
     
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
     
            <Menu
                ItemsSource="{Binding}"/>
     
            <StackPanel
                Grid.Row="1">
                <Button
                    Content="Toggle Enable"
                    Click="Enable_Click"/>
            </StackPanel>
     
        </Grid>
    </Window>

    In our code-behind for the window we build a collection of MyBusinessObject’s.  We then assign that collection to the DataContext of our Window.  This allows the Menu control to bind its ItemsSource to the current value of the DataContext (ItemsSource="{Binding}").  Where we do the magic is in the ContenPresenter_Loaded event handler.  This is fired when the MenuItems are being built.  Inside the handler we get a reference to the ContenPresenter then walk up the tree to it’s Parent MenuItem.  UtilityMethods.FindParent accomplishes this task by using the VisualTreeHelper and recursively walking the tree.  We then can get a reference to our MyBusinessObject through the DataContext of the MenuItem.  This allows us to build bindings, in code, to bind to various properties.  It’s important to remember that we can do lots of the same things in code in which we can do in XAML.  XAML is just usually funner.  =]

    public partial class Window1 : Window
    {
        private ObservableCollection<MyBusinessObject> _options;
     
        public Window1()
        {
            InitializeComponent();
     
            _options = new ObservableCollection<MyBusinessObject>();
     
            // build simple menu structure
            MyBusinessObject menuTwo = new MyBusinessObject
            {
                MenuText = "Two",
            };
     
            menuTwo.Children.Add(
                new MyBusinessObject
                {
                    MenuText = "Child 1"
                }
            );
            menuTwo.Children.Add(
                new MyBusinessObject
                {
                    MenuText = "Child 2",
                    IsEnabled = false
                }
            );
     
            _options.Add(
                new MyBusinessObject
                {
                    MenuText = "One"
                }
            );
            _options.Add(menuTwo);
     
            DataContext = _options;
        }
     
        private void ContentPresenter_Loaded(object sender, RoutedEventArgs e)
        {
            // get the content presenter
            ContentPresenter contentPresenter =
                sender as ContentPresenter;
     
            // get reference to menu item
            MenuItem menuItem =
                UtilityMethods.FindParent<MenuItem>(contentPresenter);
     
            // get reference to business object
            MyBusinessObject myBusinessObject =
                menuItem.DataContext as MyBusinessObject;
     
            // create bindings
            Binding isEnabledBinding = new Binding("IsEnabled");
            isEnabledBinding.Mode = BindingMode.TwoWay;
            isEnabledBinding.Source = myBusinessObject;
            menuItem.SetBinding(MenuItem.IsEnabledProperty, isEnabledBinding);
     
            Binding commandBinding = new Binding("Command");
            commandBinding.Mode = BindingMode.TwoWay;
            commandBinding.Source = myBusinessObject;
            menuItem.SetBinding(MenuItem.CommandProperty, commandBinding);
        }
     
        private void Enable_Click(object sender, RoutedEventArgs e)
        {
            // toggle IsEnabled
            _options[1].Children[1].IsEnabled =
                !_options[1].Children[1].IsEnabled;
        }
    }

    And that’s it!  I hope that helps!

    Download Source:  WPFDataBoundMenu.zip

    Joe

    Posted Nov 25 2008, 06:49 PM by Joe's Blog
    Filed under: ,
  • UI Design Patterns Presentation Code + Slide Deck

    Thank you to everyone who came to my presentation on UI Design patterns!  It was great to see so many people interested in patterns for building user interfaces.

    One user group member recorded the presentation on his laptop's webcam; I should have that available for download this weekend.  The video is so-so, but the audio isn't too bad.  Thanks Walt!

    Links

    Download Code & Slides - http://xamlcoder.com/joe/downloads/UIDesignPatterns2008.zip

    Martin Fowler MVC
    Martin Fowler MVP
    ASP.NET MVP & Other Patterns
    Karl Shifflett MVVM

    Requirements

    • Visual Studio 2008 SP1
    • Silverlight Tools for Visual Studio
    • NUnit (included with code)
    • Rhino Mocks (included with code

    There's a small "gotcha" with the Silverlight sample.  The implemenation is slightly different than that of it's WPF counterpart.  Silverlight Dependency properties do not automatically fire change notifications when their values have changed.  Therefore if you want property change notifications your control (read DependencyObject which the Dependency Property is a member of) itself has to implement the INotifyPropertyChanged interface, or you can do what I've done here and set the DataContext of a container control.

  • Utah .NET User Group Presentation - UI Design Patterns

    Join me this week at the Utah .NET User Group where I’ll be giving a presentation on UI Design Patterns.

    UI Design Patterns

    In this session we’ll explore three UI design patterns: Model View Controller (MVC), Model View Presenter (MVP), and Model View ViewModel (MVVM).  We’ll prove that you can reuse the same logic to drive a WinForms, ASP.NET, WPF, or Silverlight "View".  We’ll also see how using these patterns allow the bulk of your UI to be testable using testing frameworks such as NUnit and Rhino Mocks.

    Date: Thursday, November 13th, 2008

    Time: 6:00 PM

    Place: Neumont University 3rd Floor (10701 South River Front Parkway, South Jordan)

    See you there!

  • Building composite Applications Using PRISM Presentation

    Thank you to everyone who attended my presentation on PRISM at the Utah Code Camp this past weekend.  Here’s a link to my sample code and slide deck.

    http://xamlcoder.com/joe/downloads/PRISM-UTCodeCamp2008.zip

    Note that you’ll need VS2008 SP1 with the Silverlight Tools for Visual Studio 2008 SP1 installed to run the Silverlight demo.

  • Composite Silverlight 2.0 Application Library Updated to RTW

    I’ve updated my demo of the Composite Silverlight Library I built to work with Silverlight RTW.  The P&P group has plans to ship the next version of the PRISM library (PRISM 2.0) that supports Silverlight, so I’ve started to use their bits instead of the ones that I built with this sample.  Bellow is a screenshot of the conversion.  As you can see, the default styles in Silverlight 2.0 have been changed, and the rendering is much better.

    Download:  CompositeSilverlightRTW.zip

    Composite Silverlight (PRISM) Region Quickstart

  • Building a Silverlight ComboBox Using Attached Behaviors

    I recently needed to use a ComboBox in an application I was writing.  Because there is no built-in ComboBox in Silverlight I decided to explore building one using attached behaviors.  If you’re not familiar with this design pattern, check out Nikhil’s posts.  My ComboBox behavior is loosely based on his AutoComplete behavior.  I also make use of Julian’s ButtonCommands class, which he describes in this post.

    Silverlight ComboBox Project Test Page - Mozilla Firefox

    I started with a simple behavior interface:

    /// <summary>
    /// Represents a contract for encapsulation of logic that can be added
    /// to a dependency object through a pattern of attachment.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IBehavior<T> where T : DependencyObject
    {
        /// <summary>
        /// Gets the associated object.
        /// </summary>
        /// <value>The associated object.</value>
        T AssociatedObject { get; }
     
        /// <summary>
        /// Attaches to the specified associated object.
        /// </summary>
        /// <param name="associatedObject">The associated object.</param>
        void Attach(T associatedObject);
     
        /// <summary>
        /// Detaches from the associated object.
        /// </summary>
        void Detach();
    }

    Which I used to implement a ComboBoxBehavior class.  I chose to use FrameworkElement because I wanted to be able to attach this ComboBox to different controls.  Possibly a TextBlock, a TextBox, a Border … any number of controls.  We’ll come back to this later …

    public class ComboBoxBehavior : IBehavior<FrameworkElement>
    {
        ...
    }

    After creating the behavior I created a simple static class which had a single attached property of type ComboBoxBehavior.

    public static class Behaviors
    {
        public static readonly DependencyProperty ComboBoxProperty =
            DependencyProperty.RegisterAttached(
                "ComboBox",
                typeof(ComboBoxBehavior),
                typeof(Behaviors),
                new PropertyMetadata(
                    new PropertyChangedCallback(OnComboBoxChanged)
                )
            );
        ...
    }

    Attach() and Detach() are called in the property changed callback.

    private static void OnComboBoxChanged(
        FrameworkElement element,
        ComboBoxBehavior oldValue,
        ComboBoxBehavior newValue)
    {
        if (oldValue != null)
        {
            oldValue.Detach();
        }
     
        if (newValue != null)
        {
            newValue.Attach(element);
        }
    }

    You can then wire up the behavior in XAML … I’m using a Border control in this example.

    <Border
        xmlns:local="clr-namespace:SilverlightComboBox">
        <local:Behaviors.ComboBox>
            <local:ComboBoxBehavior
                Opened="SimpleComboBoxBehavior_Opened"
                ItemSelected="SimpleComboBoxBehavior_ItemSelected"
                ShowComboBoxCommand="{StaticResource ShowComboBoxSimple}">
            </local:ComboBoxBehavior>
        </local:Behaviors.ComboBox>
        <TextBlock
            x:Name="uxSimpleComboBox"
            Margin="4,0,4,0">
        </TextBlock>
    </Border>

    So onto the workhorse … the behavior itself.  The way I built this behavior to work is it fires an event, Opened,  when the DropDown is opened, in which you can set the ItemsSource and the SelectedIndex of the ItemsSource, as demonstrated in the following code:

    private void SimpleComboBoxBehavior_Opened(
        object sender, ComboBoxOpenedEventArgs e)
    {
        List<string> strings = new List<string>();
        strings.Add("A");
        strings.Add("B");
        strings.Add("C");
        strings.Add("D");
     
        e.ItemsSource = strings;
     
        var index = strings.IndexOf(uxSimpleComboBox.Text);
        e.SelectedIndex = index;
    }

    Once the user selects a value another event is fired, ItemSelected, which can also be handled:

    private void SimpleComboBoxBehavior_ItemSelected(
        object sender, ComboBoxItemSelectedEventArgs e)
    {
        if (e.SelectedItem != null)
        {
            uxSimpleComboBox.Text = e.SelectedItem.ToString();
        }
    }

    You may be wondering why I took this approach.  I decided to go down the eventing route because as I mentioned previously, I wanted to be able to attach this behavior to lots of different controls.  As such I felt that it should be up to the implementer to display the data as needed.

    I also decided to use commands to drive the opening and closing of the DropDown.  This allows the user to declaratively specify when the DropDown is opened or closed.  If you look at the XAML for the behavior I’m wiring up a ShowComboBoxSimple command to the ShowComboBoxCommand.

    ShowComboBoxCommand="{StaticResource ShowComboBoxSimple}"

    This is an ICommand of type MultiDelegateCommand.  MultiDelegateCommand is a command which can register multiple delegates to call when it is executed.

    I registered a MultiDelegateCommand instance in the Application Resources to be able to use it in the markup:

    public Page()
    {
        Application.Current.Resources.Add(
            "ShowComboBoxSimple",
            new MultiDelegateCommand()
        );
     
        ...
     
        InitializeComponent();
     
        ... 
    }

    Lastly, I wanted to show the DropDown anytime any portion of the control I’m attaching to is clicked.  This is accomplished by subscribing to the MouseLeftButtonUp event when the associated object is being attached:

    /// <summary>
    /// Attaches to the specified associated object.
    /// </summary>
    /// <param name="associatedObject">The associated object.</param>
    public void Attach(FrameworkElement associatedObject)
    {
        AssociatedObject = associatedObject;
        AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
        AssociatedObject.LostFocus += AssociatedObject_LostFocus;
    }
    private void AssociatedObject_MouseLeftButtonUp(
        object sender, MouseButtonEventArgs e)
    {
        if (ShowComboBoxCommand != null && !IsDropDownOpen)
        {
            ShowComboBoxCommand.Execute();
        }
    }

    And that’s basically it!  You may be wondering ... does this control support complex types?  You betcha! The source show’s a sample of working with complex types.  Here is shown a complex City type with a Name property.

    <DataTemplate x:Key="CityTemplate">
        <TextBlock
            Text="{Binding Name}"
            ToolTipService.ToolTip="{Binding Name}"/>
    </DataTemplate>
     
    ...
     
    <local:Behaviors.ComboBox>
        <local:ComboBoxBehavior
            Opened="ComplexComboBoxBehavior_Opened"
            ItemSelected="ComplexComboBoxBehavior_ItemSelected"
            ShowComboBoxCommand="{StaticResource ShowComboBoxComplex}">
            <local:ComboBoxBehavior.DropDownTemplate>
                <DataTemplate>
                    <ListBox ItemTemplate="{StaticResource CityTemplate}" />
                </DataTemplate>
            </local:ComboBoxBehavior.DropDownTemplate>
        </local:ComboBoxBehavior>
    </local:Behaviors.ComboBox>
    ...

     

    Download: SilverlightComboBox.zip

    Hope that helps!

    Joe

  • Visual Studio 2008 Service Pack 1 (SP1) Released

    Today Microsoft made Visual Studio 2008 Service Pack 1 (SP1) available for download.  I was able to participate in a case study done on SP1 with Microsoft, Misys, and Veracity Solutions specifically using ADO.NET Data Services and the Entity Framework.  You can read the full case study here, though here’s a small snippet:

    “For more than a decade, Misys Healthcare Systems and Veracity Solutions have partnered to develop innovative applications that meet the needs of healthcare providers while improving the quality of patient care. To help medical staff reduce manual, paper-based processes, Misys Healthcare Systems and Veracity Solutions collaborated to create FreeNatal, a Web-based application that provides prenatal care providers with an easy-to-use, secure interface for managing patients’ records. Using Microsoft® Visual Studio® 2008 SP1 and the Microsoft .NET Framework 3.5 SP1, eight members from the Misys-Veracity team created the application. By taking advantage of these powerful technologies, the team increased development speed by 60 percent, enabling accelerated market delivery and further strengthening their respective positions in the healthcare informatics industry.”

     FreeNatal “Face Sheet” view.

    FreeNatal “Face Sheet” view.

     FreeNatal application architecture diagram.

    FreeNatal application architecture diagram.
     
  • Composite Silverlight 2.0 Beta 2 Application Library

    Lately I’ve been working with Composite WPF and Silverlight applications at Veracity Solutions.  I spent a few hours over the weekend converting the Composite WPF Application Library to Silverlight.  Tonight I finished porting one of the QuickStarts to Silverlight.  Of course, just today the P&P team posted a AGCompositeApplicationLibrary spike.  Just my luck!  Regardless, here’s the UIComposition QuickStart running in Silverlight using my ported libraries:

    Region Quickstart - Windows Internet Explorer

    Some things to note:

    • The TabItem style isn’t using binding to get the header value.
    • Location tab doesn’t include the map data.
    • I added a TabControlRegionAdapter class that works with the Silverlight TabControl.

    Special thanks to Michael Sync for posting his Silverlight Unity port, it got me started.

    Download: CompositeSilverlight.zip

  • Building Custom Template-able WPF Controls

    Building custom controls in WPF can provide you with lots of flexibility.  It allows you to entirely separate the behavior of the control from the look of the control.  This is the premise behind most of what WPF offers.  In this post I will show you how you can build a simple control similar to the search control in Outlook 2007.

     Filter TextBox

    Add a new WPF Application project.

    New Project

    Then add a WPF User Control Library.

    Add New Project

    Delete the generated UserControl1.xaml that was given to you.

    Microsoft Visual Studio

    Add a new WPF Custom Control.

    Add New Item - FilterTextBox

    Your solution should now look like this:

    Solution Explorer

     

    The template gives you a FilterTextBox.cs and Generic.xaml file.

    public class FilterTextBox : Control
    {
        static FilterTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FilterTextBox),
                new FrameworkPropertyMetadata(typeof(FilterTextBox)));
        }
    }

     

    The default Generic.xaml is the default look for your custom control, and is found in the Theme directory.  It is just an empty border for now:

    <Style TargetType="{x:Type local:FilterTextBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type local:FilterTextBox}">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

     

    Now lets start building the behavior of our control.  We'll start by adding a 'Text' dependency property.  This will be the filter text that the user types in.  Notice that I've created callbacks to be notified when the property has changed.  And as always, do NOT put any code within the getter and setter of the CLR property, because WPF bypasses this property at runtime and calls GetValue and SetValue directly.  However the CLR property is still needed to use the property in xaml.

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text",
                                    typeof(String),
                                    typeof(FilterTextBox),
                                    new UIPropertyMetadata(null,
                                        new PropertyChangedCallback(OnTextChanged),
                                        new CoerceValueCallback(OnCoerceText))
                                   );
     
    private static object OnCoerceText(DependencyObject o, Object value)
    {
        FilterTextBox filterTextBox = o as FilterTextBox;
        if (filterTextBox != null)
            return filterTextBox.OnCoerceText((String)value);
        else
            return value;
    }
     
    private static void OnTextChanged(DependencyObject o,
                                      DependencyPropertyChangedEventArgs e)
    {
        FilterTextBox filterTextBox = o as FilterTextBox;
        if (filterTextBox != null)
            filterTextBox.OnTextChanged((String)e.OldValue, (String)e.NewValue);
    }
     
    protected virtual String OnCoerceText(String value)
    {
        return value;
    }
     
    protected virtual void OnTextChanged(String oldValue, String newValue)
    {
    }
     
    public String Text
    {
        // IMPORTANT: To maintain parity between setting a property in XAML
        // and procedural code, do not touch the getter and setter inside
        // this dependency property!
        get
        {
            return (String)GetValue(TextProperty);
        }
        set
        {
            SetValue(TextProperty, value);
        }
    }    

     

    We'll want users of the control to be notified when the text in our TextBox has changed, so lets create a 'TextChanged' event.

    public static readonly RoutedEvent TextChangedEvent =
        EventManager.RegisterRoutedEvent("TextChanged",
                                        RoutingStrategy.Bubble,
                                        typeof(RoutedEventHandler),
                                        typeof(FilterTextBox));
     
    public event RoutedEventHandler TextChanged
    {
        add { AddHandler(TextChangedEvent, value); }
        remove { RemoveHandler(TextChangedEvent, value); }
    }

     

    Now lets go back and modify our OnTextChanged method to raise our TextChanged event.

    protected virtual void OnTextChanged(String oldValue, String newValue)
    {
        // fire text changed event
        this.RaiseEvent(new RoutedEventArgs(FilterTextBox.TextChangedEvent, this));
    }

     

    With the base behavior mostly done, we can move on to creating a generic look for our control.  Lets add a DockPanel with a Button and a TextBox, the Button docked to the right.  Lets also bind the 'Text' property of the TextBox to the 'Text' property of our control.  We want the 'UpdateSourceTrigger' to be 'PropertyChanged' so that the 'TextChanged' event we created will be fired every time the user types something into the TextBox.  Notice that we don't want the TextBox to have a border because then we'd have two borders.

    <ControlTemplate TargetType="{x:Type local:FilterTextBox}">
      <Border
          Background="{TemplateBinding Background}"
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}"
          CornerRadius="3">
        <DockPanel
          LastChildFill="True"
          Margin="1">
          <Button
            x:Name="PART_ClearFilterButton"
            Content="X"
            Width="20"
            ToolTip="Clear Filter"
            DockPanel.Dock="Right" />
          <TextBox
            x:Name="PART_FilterTextBox"
            Text="{Binding Path=Text,
                          Mode=TwoWay,
                          UpdateSourceTrigger=PropertyChanged,
                          RelativeSource={RelativeSource TemplatedParent}}"
            BorderBrush="{x:Null}"
            BorderThickness="0"
            VerticalAlignment="Center" />
        </DockPanel>
      </Border>
    </ControlTemplate>

     

    Take special note of the names of the controls.  They both begin with 'PART_'.  This is the standard way in WPF to signify controls that need to be replaced if you decide to change the template of the control.  If someone templates your control, you need some way to identify that the types used for your parts need to be ones that your control can use.  You can do this by using the TemplatePart attribute and giving it the name and type of your control parts.

    [TemplatePart(Name = "PART_FilterTextBox", Type = typeof(TextBox))]
    [TemplatePart(Name = "PART_ClearFilterButton", Type = typeof(Button))]
    public class FilterTextBox : Control
    {
        ...
    }

     

    We only want the 'Clear Filter' button to be showing when there is text in the TextBox, so lets create a DataTrigger to accomplish that for us.

    <ControlTemplate TargetType="{x:Type local:FilterTextBox}">
      <Border>
        ...
      </Border>
      <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Text.Length, ElementName=PART_FilterTextBox}" Value="0">
          <Setter TargetName="PART_ClearFilterButton" Property="Visibility" Value="Collapsed" />
        </DataTrigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>

     

    Now we want to be able to handle when a user clicks on the 'Clear Filter' button.  You do this by overriding the OnApplyTemplate method.  You can get a reference to your controls by calling GetTemplateChild and passing the name of the control.  When the user clicks the 'Clear Filter' button we just want to remove any text in the TextBox.  We'll use the same strategy to get a reference to the TextBox control.

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
     
        Button clearFilterButton = base.GetTemplateChild("PART_ClearFilterButton") as Button;
        if (clearFilterButton != null)
        {
            clearFilterButton.Click += new RoutedEventHandler(ClearFilterButton_Click);
        }
    }
     
    private void ClearFilterButton_Click(Object sender, RoutedEventArgs e)
    {
        TextBox textBox = base.GetTemplateChild("PART_FilterTextBox") as TextBox;
     
        if (textBox != null)
        {
            textBox.Text = String.Empty;
        }
    }

     

    In the WPF Application project add a reference to the custom control project.

    Add Reference

    Now you can create a namespace for the controls project, labeled as controls here, and add your control to the form.

    <Window
        x:Class="FilterTextBoxDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:FilterTextBox;assembly=FilterTextBox"
        Title="FilterTextBox Demo"
        Height