Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:29:41+00:00 2026-05-24T17:29:41+00:00

In WPF, I am creating a simple custom control for my TODO program. It

  • 0

In WPF, I am creating a simple custom control for my TODO program. It should do the following:

  • Show up as a ListBox with an Add and Remove button above it.
  • The Add and Remove buttons should add and remove items from my base class.
    • I have this working
  • On pressing F2, I want the list box items to change into a TextBox control.

My main issues/Questions are:

  • On OnKeyDown, I get the error: This operation is valid only on elements that have this template applied. How do I get around this? This is where I want to press F2, and to be able to make the TextBox visible and Label invisible.
  • Is there a better way to do this? If my control is a container for buttons AND a text box, should I not be inheriting from ListBox? Should I inherit from Control instead and make it a container?

    • If so, how do I implement it? Do I basically use the following code in the style, and remove the overrides like OnKeyDown and instead register the KeyDown event for the Textbox? I’ll give that a try after this post but let me know if you have any advice.

I had something close working before, in the following code, but now I want this moved from my main Window XAML to the custom control code, and I want to be able to edit on button press:

<!-- In my MainWindow.XAML -->
<TaskDashControls:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3"
        ItemsSource="{Binding}">
    <TaskDashControls:ListBoxWithAddRemove.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Button DockPanel.Dock="Left" Click="SelectItemClick">SELECT</Button>
                <TextBlock x:Name="LabelDescription" Visibility="Visible"  DockPanel.Dock="Left" Text="{Binding Description}" Height="25" Width="150" />
                <TextBox x:Name="EditableDescription" Visibility="Collapsed" DockPanel.Dock="Left" Text="{Binding Description}" Height="25" Width="150" />
                <Button DockPanel.Dock="Left" Click="EditTaskItemClick">EDIT</Button>
            </DockPanel>
        </DataTemplate>
    </TaskDashControls:ListBoxWithAddRemove.ItemTemplate>
</TaskDashControls:ListBoxWithAddRemove>

I have now removed the DataTemplate, to move it over to the custom control:

<!-- In my MainWindow.XAML -->
<TaskDashControls:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3"
        ItemsSource="{Binding}"/>

Here is the custom control in Generic.XAML:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TaskDash.Controls">


<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />


<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="2" />
</Style>


<Style x:Key="{x:Type local:ListBoxWithAddRemove}" TargetType="{x:Type local:ListBoxWithAddRemove}">
    <Setter Property="Margin" Value="3" />
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="25" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <!--<Button Grid.Column="0" Grid.Row="0" x:Name="DeleteButton"
                                    Click="DeleteControlClick">Delete</Button>
                    <Button Grid.Column="1" Grid.Row="0" x:Name="AddButton"
                                    Click="AddControlClick">Add</Button>-->
                    <Button Grid.Column="0" Grid.Row="0" x:Name="DeleteButton">Delete</Button>
                    <Button Grid.Column="1" Grid.Row="0" x:Name="AddButton">Add</Button>
                    <Border 
                                Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
                                  Name="Border" 
                                  Background="{StaticResource WindowBackgroundBrush}"
                                  BorderBrush="{StaticResource SolidBorderBrush}"
                                  BorderThickness="1"
                                  CornerRadius="2">
                        <ScrollViewer 
                                    Margin="0"
                                    Focusable="false">
                            <StackPanel Margin="0" IsItemsHost="True" />
                        </ScrollViewer>

                        <!--<ListBox ItemTemplate="{TemplateBinding ItemTemplate}">
                            <DataTemplate>
                                <DockPanel>
                                    <TextBlock x:Name="LabelDescription" Visibility="Visible"  DockPanel.Dock="Left" Text="{Binding Description}" Height="25" Width="150" />
                                    <TextBox x:Name="EditableDescription"  DockPanel.Dock="Left" Text="{Binding Description}" Height="25" Width="150" />
                                </DockPanel>
                            </DataTemplate>
                        </ListBox>-->

                    </Border>
                </Grid>
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>

Here is my custom control class

using System;
using System.Windows;
using System.Windows.Controls;

namespace TaskDash.Controls
{
[TemplatePart(Name = "Text", Type = typeof(TextBox))]
[TemplatePart(Name = "LabelText", Type = typeof(TextBlock))]
public class TextBoxWithDescription : Control
{
    static TextBoxWithDescription()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxWithDescription), new FrameworkPropertyMetadata(typeof(TextBoxWithDescription)));
    }

    public TextBoxWithDescription()
    {
        LabelText = String.Empty;
        Text = String.Empty;
    }

    public static readonly DependencyProperty LabelTextProperty =
        DependencyProperty.Register("LabelText", typeof(string), typeof(TextBoxWithDescription),
        new PropertyMetadata(string.Empty, OnLabelTextPropertyChanged));
    private static void OnLabelTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }
    public string LabelText
    {
        get { return GetValue(LabelTextProperty).ToString(); ; }
        set { SetValue(LabelTextProperty, value); }
    }

    // http://xamlcoder.com/cs/blogs/joe/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(TextBoxWithDescription),
        new UIPropertyMetadata(null,
                                new PropertyChangedCallback(OnTextChanged)
                           ));
    private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        TextBoxWithDescription textBox = o as TextBoxWithDescription;
        if (textBox != null)
            textBox.OnTextChanged((String)e.OldValue, (String)e.NewValue);
    }
    protected virtual void OnTextChanged(String oldValue, String newValue)
    {
        // fire text changed event
        this.Text = newValue;
        this.RaiseEvent(new RoutedEventArgs(TextChangedEvent, this));
    }
    public string Text
    {
        get { return GetValue(TextProperty).ToString(); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly RoutedEvent TextChangedEvent =
        EventManager.RegisterRoutedEvent("TextChanged",
                                RoutingStrategy.Bubble,
                                typeof(RoutedEventHandler),
                                typeof(TextBoxWithDescription));
    public event RoutedEventHandler TextChanged
    {
        add { AddHandler(TextChangedEvent, value); }
        remove { RemoveHandler(TextChangedEvent, value); }
    }



    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        //var textBlock = (TextBlock)this.Template.FindName("LabelText", this);
        //if (textBlock != null) textBlock.Text = this.LabelText;


        //var textBox = (TextBox)this.Template.FindName("Text", this);
        //if (textBox != null) textBox.Text = this.Text;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:29:42+00:00Added an answer on May 24, 2026 at 5:29 pm

    I would create a class for TODO with Properties: String Desc, Visibility txtBox, Visibility txtBlock. Then items source for the TaskDashControls is List TODO. In xaml you can bind the visibilty property. In the TODO class you can control that when one is visible the other is not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a WPF custom control, a Button with an Image and Text
We're creating a WPF custom control targeted at .NET 3.5. It is very compelling
I am creating a custom control derived from the one of the Standard WPF
I am creating a WPF Application using VS 2005. I download the add on
I'm trying to create a GUI (WPF) Library where each (custom) control basically wraps
I'm into creating a simple contact management application to test how WPF works with
I lately had the problem of creating add and edit dialogs for my wpf
Im creating a simple vehicle and projectile simulation game in WPF using C#. I
I'm creating a simple drawing application using WPF. I've run into some strange behaviour
I'm creating a simple WPF print dialog box to setup a label printer. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.