This is a migrated thread and some comments may be shown as answers.

How to create a Close button in a User Control

13 Answers 3036 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
herb
Top achievements
Rank 1
Veteran
Iron
herb asked on 07 Dec 2019, 07:38 PM

Have a User Control that is called when a button "about" is pressed.

The UC works. Want to add a "Close" button that will close the window so it won't get orphaned later.

I could use the "IsVisible = Visibility.Collapsed;" which works but it is not really gone. The UC has to RadListboxes which collects different data in each box. When I use the "IsVisible = Visibility.Collapsed;" the box disappears. When click on the "About" button it reappears with previous data. I prefer to make it truly close/destroyed.

Is this possible and if so how? please.

What code do you need the XAML or the .CS?

13 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Dec 2019, 03:04 PM

Hello Herb,

If you want to remove a control (like RadListBox for example) from the view entirely, you can remove it from the parent element that hosts it. For example, if the RadListBox is hosted in a Grid panel, you can cremove the control from its Children collection.

myGrid.Children.Remove(myListBoxInstance);

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 11 Dec 2019, 10:05 PM

I should have posted some code from my project for clarity. Project is MVVM formatted. The "OnCloseCommand()" in the AboutViewModel.cs is where I want the user control window to be closed.

From the MainWindow.xaml:

<telerik:RadButton x:Name="btnAbout"
                   Margin="2,0,0.5,1.5"
                   Command="{Binding AboutButtonCommand}"
                   Visibility="Visible">
    <StackPanel Orientation="Horizontal">
        <TextBlock x:Name="btnImgAbout"
                   Margin="5,0,0,0"
                   FontSize="25"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Text=""
                   Foreground="#FFD294EA"
                   FontFamily="/VoicemailAnalyticsWpf;component/Resources/#Segoe MDL2 Assets" />
        <TextBlock Margin="5,0,0,0"
                   FontSize="16"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"><Run Text="About" /></TextBlock>
    </StackPanel>
</telerik:RadButton>

From the AboutViewModel:

using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Windows;
 
namespace Wpsi.VmaUtilities.ViewModels
{
    public class AboutViewModel : ViewModelBase
    {
        //build code to collect data into a list box.
        //Filename/tVersion
        /// <summary>GetProgramData()
        /// Find the path of the application.
        /// Count the number of files.
        /// Populate the array list to pass to the GetVersion.
        /// </summary>
         
 
        //private string _VersionList;
        private ObservableCollection<string> _versionList;
        private Visibility _isVisible = Visibility.Collapsed;
 
        #region RelayCommands
        public RelayCommand RefreshListCommand
        {
            get;
            set;
        }
 
        public RelayCommand CloseCommand
        {
            get;
            set;
        }
        #endregion RelayCommands
 
        #region Properties
        string sFile
        {
            get; set;
        }
        public ObservableCollection<string> VersionList
        {
            get => _versionList;
            set
            {
                if (_versionList == value)
                {
                    return;
                }
                _versionList = value;
                RaisePropertyChanged(nameof(VersionList));//This name must match the name in the "ItemsSource="{Binding MrsUserDataTable}" of MrsUserSettings.xaml
                //for this to work.
            }
        }
 
        public Visibility IsVisible
        {
            get => _isVisible;
            set
            {
                _isVisible = value;
                RaisePropertyChanged(nameof(IsVisible));
            }
        }
        #endregion Properties
 
 
        public AboutViewModel()
        {
            RefreshListCommand = new RelayCommand(OnRefreshListCommandExecute, () => true);
            CloseCommand = new RelayCommand(OnCloseCommand, () => true);
        }
 
        private List<string> GetProgramData()
        {
            DirectoryInfo fSet = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory);
            string ProgramPath = Path.GetDirectoryName(fSet.ToString());
 
            //get list of dlls and exe for display
            FileInfo[] saFiles = fSet.GetFiles();
            string[] aFiles = new string[saFiles.Count()];
            int i = 0;
             
            foreach (FileInfo f in saFiles)
            {
                aFiles[i] = ProgramPath + "\\" + f.Name;
                i++;
            }
            return GetVersion(aFiles);
        }
        /// <summary>GetVersion(string[] aFiles)
        /// Receive the array list of files found in the application path to reach "dll" and "exe"
        /// version details to put into the user control's RadListbox.
        /// Using Telerik's list box to show the file list.
        /// </summary>
        /// <param name="aFiles">
        /// An array of the files found in the program path
        /// </param>
        /// <returns>
        /// List<string> lsVersionList to the RadListbox control
        /// </returns>
        private List<string> GetVersion(string[] aFiles)
        {
            List<string> lsVersionList = new List<string>();
            FileVersionInfo vmaFileVersion = null;
 
            foreach (string sService in aFiles)
            {
                if (sService.IndexOf(".exe") > 0)
                {
                    vmaFileVersion = FileVersionInfo.GetVersionInfo(sService);
                    lsVersionList.Add(Path.GetFileName(vmaFileVersion.FileName) + "\t" + vmaFileVersion.FileVersion);
                }
                if (sService.IndexOf(".dll") > 0)
                {
                    vmaFileVersion = FileVersionInfo.GetVersionInfo(sService);
                    lsVersionList.Add(Path.GetFileName(vmaFileVersion.FileName) + "\t" + vmaFileVersion.FileVersion);
                }
            }
 
            return lsVersionList;
        }
 
        public void OnRefreshListCommandExecute()
        {
            VersionList = new ObservableCollection<string>(GetProgramData());
        }
        public void OnCloseCommand()
        {
            //Now Close the control
            IsVisible = Visibility.Collapsed;
             
        }
 
        //private List<string> GetXpres()
        //{
        //  return;
        //}
 
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }//class AboutViewModel
}//namespace Wpsi.VmaUtilities.ViewModels
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 12 Dec 2019, 01:22 PM
I want to remove the UC window box that is called when the "about" button is clicked. I can make the "about" dialog box become invisible but I prefer to close it so if the program crashes it will not be left behind.
0
Martin Ivanov
Telerik team
answered on 16 Dec 2019, 11:08 AM

Hello Herb,

Thank you for the additional information.

To achieve your requirement, you can use an approach with an attached property. For example, you can define a property called IsClosed which can be bound to the IsVisible of the AboutViewModel class. In the property changed callback of the attached, you can call the Close() method of the dialog window. I've attached a small example showing this approach. I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 16 Dec 2019, 05:59 PM

Hello Martin, thank you for your sample.

Not able to step through it because the "About" button is no seen and not calling the About window.

0
Martin Ivanov
Telerik team
answered on 17 Dec 2019, 12:53 PM

Hello Herb,

The project is using Xaml dlls. Can you please double check if it reference those? If the dlls are NoXaml, nothing will be shown. You can check the Missing Controls in the UI article for more information on this topic.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 21 Dec 2019, 08:25 PM

Sorry for the long delay. Had to jump off this project.

I reviewed you code/project. I see that you are closing an item.

However I'm wishing to close a User Control Window I created to display my "About" RadListboxes.

I know I can use the code below to hide the UCW buy still not clear on how to close the UCW instead of just hide it.

There is no "X" in the control when you create a new window dialog. Closing the UCW in the UCW seems needing something I don't understand.

public void OnCloseCommand()
{
    //Now Close the control
    IsVisible = Visibility.Collapsed;
     
}

0
Dinko | Tech Support Engineer
Telerik team
answered on 25 Dec 2019, 11:24 AM

Hi Herb,

My name is Dinko, and I'm stepping into the conversation for my colleague Martin, who is currently out of the office.

Reading your last post, I am not sure that I was able to understand your approach fully. I have taken a look at the project provided by Martin, and a Window element is closed when the button inside is pressed. 

Is it possible to modify the project send from Martin to mimics your implementation and send it back to us? This way, we can have a better look and provide you with a suitable solution.

Regards,
Dinko
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 25 Dec 2019, 04:35 PM
I will do. I will get back as soon as I can. 
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 28 Dec 2019, 06:41 PM

Ok here is a sample of what I have.

When you click on the "About" button a user control "About.xaml" is called and creates the version list Radlistbox.

There are three buttons "Close", "Save to File" and "Refresh".

When the "Refresh" is click it will collect the version details in the application file.

The button I'm interesting (now this could be my ignorance as what should happen) when press all the button does is make the user control About.xaml invisible. In my WinForms experience on some exceptions a form can be orphaned. I want to "close" "destroy" the user control.

Is this possible from within the user control?

0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 28 Dec 2019, 06:44 PM

oops. here you go.

Can't add "zip" so rename ".jpg" to "zip"

0
Accepted
Martin Ivanov
Telerik team
answered on 30 Dec 2019, 11:52 AM

Hello Herb,

To achieve your requirement you will need to use a slightly different approach than setting the Visibility property of the About UserControl. The Visibility is useful if you need to have the control presented all of the time (hidden or not). However, to create and remove the UserControl from memory, you will need to remove it from its parent host (the Grid panel in your case). There is no straightforward approach that can be applied with the current setup, but you should be able to achieve your requirement by replacing the visibility approach with the other one (removing the child from the host).

I've attached a project showing a possible approach to implement this.

Another approach is to keep using the visibility and just make sure that everything in the view model is clean up when the control is hidden.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 30 Dec 2019, 04:37 PM

Thank you Martin.

I will review you sample code and if I have further question update you.

Tags
ListBox
Asked by
herb
Top achievements
Rank 1
Veteran
Iron
Answers by
Martin Ivanov
Telerik team
herb
Top achievements
Rank 1
Veteran
Iron
Dinko | Tech Support Engineer
Telerik team
Share this question
or