0

down vote
favorite I am a programmer for winforms and asp.net. A new-comer to WPF application development.

I wish to thank in advance for any help rendered to my query,

I am working on a application for Banking. My job profile is to develop reusable usercontrols for the application.

An example would be to make a Simple numeric input box which has Max and Min number properties. So the control pops an error if the number entered is not within a range. (ofcourse there is much more to this control :-)

On similar lines. I have to develop a listview user control with Two properties.

  1. A comma separated values of column headers
  2. A comma separated values of data to add

My colleague, now places this usercontrol on window (as known in WPF). and passes the above parameters to the listview control.

My coding ensures that the columns are placed and data is added. In winforms this is a simple process. In WPF i was able to achieve it for known columns by defining beforehand in observablecollection or otherwise using

SamplelistView.Items.Add(new { FirstName = "test1", LastName = "ABX", EmployeeNumber = "AAA111"});

However in the above mentioned command, Firstname,Lastname are previously known columns.

Is there any command like,adding data as a array or adding data directly to columns without binding to a collection. OR is there a way to add columns to observablecollections at runtime.

Pls. excuse my limited exposure on nomenclature and otherwise.

For your first question, there are a few methods. If you are not using binding, then you could handle the TextChanged event, although this can be quite messy. If, however, you are using binding, you can perform validation in two possible locations: at the source or at the target. The target being your control.

In order to validate from the target, you would first have to create a custom validation rule by deriving from the ValidationRule class, for example:

public class RangeValidation : ValidationRule
{
    private double _max;
    private double _min;
    public double Max
    {
        get
        { return _max; }
        set
        { _max = value; }
    }
    public double Min
    {
        get
        { return _min; }
        set
        { _min = value; }
    }
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        try
        {
            double val = double.Parse(value as string);
            if (val > Max)
                return new ValidationResult(false, "Value is greater than the max value.");
            if (val < Min)
                return new ValidationResult(false, "Value is less than the min value.");
            return new ValidationResult(true, null);
        }
        catch (Exception exc)
        {
            return new ValidationResult(false, exc);
        }
    }
}

Then you would add it to your Binding for TextBox.Text:

<Binding.ValidationRules>
    <local:RangeValidation Max="100.0" Min="0.0"/>
</Binding.ValidationRules>

You could setup your Min and Max values as DependencyProperty's of DependencyProperty's of your UserControl.

It doesn't sound like you would be validating from the source, however if you were, you would simply add a ValidateValueCallbackHandler in the DependencyProperty.Register method.

For your second control, I would first like to say that I highly recommend against this. In most cases, perhaps not yours, the name/number of columns is static, or possibly changeable from a predefined list. In this case, in my opinion, each ListBox should be defined dependent of it's location and context within the application.

In either case, you can add arrays to the Items property (i.e. listView.Items.Add(new string[]{"item1", "item2"}); ). You will just have to handle the bindings in code-behind. For instance:

public void SetHeaders(params string[] headers)
{
    gridView.Columns.Clear();
    for(int i = 0; i < headers.Length; i++)
    {
        GridViewColumn column = new GridViewColumn();
        column.Header = headers[i];

        Binding binding = new Binding();
        binding.Path = new PropertyPath(string.Format("[{0}]", i));
        column.DisplayMemberBinding = binding;

        gridView.Columns.Add(column);
    }
}

I haven't tested all of the code, so I'm not sure if it all works. Let me know if you need any more clarification.

Be a part of the DaniWeb community

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