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

  • Home
  • SEARCH
  • 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 9245699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:19:24+00:00 2026-06-18T09:19:24+00:00

I have a user control which has a CheckBox , a Button , and

  • 0

I have a user control which has a CheckBox, a Button, and a CommandBinding. If the CheckBox is checked, the Button is enabled. The MainWindow uses the UserControl. When the Button in the main window is pressed, the UserControl is removed from UI, and GC.Collect() is called, but CanExecute method still runs.

I find that if I click the button in main window twice, CanExecute will no longer run. It seems that I don’t call GC.Collect() at the right time.

I want to know what is the good timing to call GC to clean the unused user control, so that CanExecute will not be called.

XAML

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <RoutedUICommand x:Key="okCommand" Text="OK"/>
    </UserControl.Resources>
    <UserControl.CommandBindings>
        <CommandBinding Command="{StaticResource okCommand}" CanExecute="CommandBinding_CanExecute_1"/>
    </UserControl.CommandBindings>
    <StackPanel>
        <CheckBox Name="checkBox" Content="CheckBox"/>
        <Button Command="{StaticResource okCommand}" Content="{Binding Path=Text, Source={StaticResource okCommand}}"/>
    </StackPanel>
</UserControl>

Code behind

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = checkBox.IsChecked.GetValueOrDefault(false);

        System.Media.SystemSounds.Beep.Play();
    }
}

MainWindow

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Loaded="Window_Loaded_1">
    <StackPanel>
        <Border Name="container"/>

        <Button Content="Set Null" Click="Button_Click_1"/>
    </StackPanel>
</Window>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        container.Child = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        UserControl1 uc = new UserControl1();
        container.Child = uc;
    }
}
  • 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-06-18T09:19:26+00:00Added an answer on June 18, 2026 at 9:19 am

    I find another solution. That is to call CommandBindings.Clear() in UserControl1 when it unloads.

    I believe this is a neat way, since the caller of UserControl1 doesn’t take care of the cleaning job of UserControl1.

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

Sidebar

Related Questions

I have created a user control (link) which has a label and a button.
I have a user control which has a textbox on it, now this usercontrol
I have a user control which has a DateTimePicker overlaid with the single line
I have a user control which uses objects as inner properties (some code is
I have a user control which has a ComboBox and a SelectedIndexChanged event handler.
I have a user control which has several radiobuttons and buttons... I have code
I have 3rd party user control (a captcha control), which has a captcha image,
To explain confusing title, here is the deal. I have user control which has
I have been trying to create a user control library which simply has styled
I have a user control which has a Canvas of height 100 and width

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.