theMonkey 0 Newbie Poster

Hi there.

I need some URGENT HELP with custom controls. I'm new to WPF. How can i create a custom control with a TextBox and label? I have one but it does not show the Label and Textbox. I want the custom control to be flexible. How can I display the Label next to the Texbox (Horizontally) and also on top (Vertically)? I have an Orientation Property but this does not work. How can i add formatting to the control, example:
1. Set decimal places and thousand separator. When entering data, the insertion point should be from the Right and should automatically move the numbers one place per number to the left, like when you enter a money value at an atm.
2. How can i format the same textbox in the Custom Control to display a date eg when a date is entered 10122010, it should display 10/12/2010 (Day/Month/Year format).
3. How can i tab to a custom control loaded dynamically? Tabbing works fine but when a control is loaded dynamically, it only tabs to the first custom control and moves to the next View. The rest of the custom controls are ignored.
4. I have an Enumeration with values, AccountNum, Amount, DatePaid, Installment. These Fields are from a Class called Statement. How can I Dynamically create a Custom Control with a Label and Textbox for each of these fields and set the Label of each Field with the values of the Enum members, so when the control is loaded dynamically, it should display AccountNum, Amount, DatePaid and Installment in the Labels.
5. Lastly, how can I select a Statement item in a ListBox and then populate the Dynamic Controls with the values of the selected item, and how can I total (sum) the numeric values in the ListBox and display it in a textbox? My code for this is below but it only displays the value of the SelectedItem and not the total of the items. Bear in mind that the ListBox is loaded, it does not only contain numeric values. It's a combination of AccountNum, Unpaid(status) and the value of the installment.

This is the calculation

void CalculateSelectedTotal(object items) {
            if (items == null || (items != null && ((System.Collections.IList)items).Count == 0)) {
                if (CurrentBatch.Count(a => a.Type == VoucherTypes.Credit) > 0) {
                    SelectedBatchCreditTotal = CurrentBatch.Where(a => a.Type == VoucherTypes.Credit).Sum(a => a.Amount);
                    SetPropertyChanged("SelectedBatchCreditTotal");
                }

                if (CurrentBatch.Count(a => a.Type == VoucherTypes.Debit) > 0) {
                    SelectedBatchDebitTotal = CurrentBatch.Where(a => a.Type == VoucherTypes.Debit).Sum(a => a.Amount);
                    SetPropertyChanged("SelectedBatchDebitTotal");
                }
            }

            var source = ((System.Collections.IList)items).Cast<VoucherItem>().ToList();
            if (source.Count(a => a.Type == VoucherTypes.Credit) > 0) {
                SelectedBatchCreditTotal = source.Where(a => a.Type == VoucherTypes.Credit).Sum(a => a.Amount);
                SetPropertyChanged("SelectedBatchCreditTotal");
            }

            if (source.Count(a => a.Type == VoucherTypes.Debit) > 0) {
                SelectedBatchDebitTotal = source.Where(a => a.Type == VoucherTypes.Debit).Sum(a => a.Amount);
                SetPropertyChanged("SelectedBatchDebitTotal");
            }

        }


I urgently need help with this please!!! Any help would really be appreciated.

My current code is this:
CustomControl:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CusomControls
{
    [TemplatePart(Name = "PART_Label", Type = typeof(Label))]
    [TemplatePart(Name = "PART_Content", Type = typeof(TextBox))]
    public class CustomControl : Control
    {        
        #region Constants

        public enum eType
        {
            Textbox
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes the <see cref="Field"/> class.
        /// </summary>
        static Field()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Field), new FrameworkPropertyMetadata(typeof(Field)));            
        }

        #endregion

        #region Properties

        #region Controls

        Label title;
        ContentControl content;

        #endregion

        /// <summary>
        /// Gets or sets the type of the control.
        /// </summary>
        /// <value>
        /// The type of the control.
        /// </value>       
        public eType ControlType
        {
            get { return (eType)GetValue(ControlTypeProperty); }
            set { SetValue(ControlTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControlTypeProperty =
            DependencyProperty.Register("ControlType", typeof(eType), typeof(Field), new UIPropertyMetadata(eType.Textbox));

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        //...
        public string dataList
        {
            get { return (string)GetValue(dataListProperty); }
            set { SetValue(dataListProperty, value); }
        }

        //Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty dataListProperty =
            DependencyProperty.Register("dataList", typeof(string), typeof(Field), new UIPropertyMetadata("dataList"));
        //...

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(Field), new UIPropertyMetadata("Title"));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Field), new UIPropertyMetadata("Something"));



        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Orientation), new 
        UIPropertyMetadata(Orientation.Vertical));
        #endregion

        #region Methods
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
        #endregion        
    }
}

Add the code for the View (which is a User Control) that holds the Custom Control:

<Grid >
        <ListView ItemsSource="{Binding CurrentBatch}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" TabIndex="1" >
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <my:CustomControl />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.