Mostrando entradas con la etiqueta Devexpress. Mostrar todas las entradas
Mostrando entradas con la etiqueta Devexpress. Mostrar todas las entradas

viernes, 21 de marzo de 2014

Setear error en las filas de un grid Devexpress

Para validar la información de un registro y si no pasa dicha validación setear el icono del error en la fila y columna del registro, hacemos algo como lo estamos haciendo en el siguiente ejemplo:

* La clase que estamos utilizando como fuente de datos, la heredamos de IDXDataErrorInfo

public class Person : object, IDXDataErrorInfo {
        public Person(string firstName, string lastName, string address, string phoneNumber, string email) {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Address = address;
            this.PhoneNumber = phoneNumber;
            this.Email = email;
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }

        #region IDXDataErrorInfo Members
        void IDXDataErrorInfo.GetPropertyError(string propertyName, ErrorInfo info) {
            switch(propertyName) {
                case "FirstName":
                case "LastName":
                    if(IsStringEmpty(propertyName == "FirstName" ? FirstName : LastName)) {
                        SetErrorInfo(info, propertyName + " field can't be empty", ErrorType.Critical);
                    }
                    break;
                case "Address":
                    if(IsStringEmpty(Address)) {
                        SetErrorInfo(info, "Address hasn't been entered", ErrorType.Information);
                    }
                    break;
                case "Email":
                    if(IsStringEmpty(Email)) {
                        SetErrorInfo(info, "Email hasn't been entered", ErrorType.Information);
                    } else if(Email != "none" && !IsEmailCorrect(Email)) {
                        SetErrorInfo(info, "Wrong email address", ErrorType.Warning);
                    }
                    break;
            }
        }
        void IDXDataErrorInfo.GetError(ErrorInfo info) {
            if(IsStringEmpty(PhoneNumber) && (Email == "none" || !IsEmailCorrect(Email)))
                SetErrorInfo(info, "Either Phone Number or Email should be specified", ErrorType.Information);
        }
        #endregion
        bool IsStringEmpty(string str) {
            return str == null || str.Trim().Length == 0;
        }
        bool IsEmailCorrect(string email) {
            return email == null || (email.IndexOf("@") >= 1 && email.Length > email.IndexOf("@") + 1);
        }
        void SetErrorInfo(ErrorInfo info, string errorText, ErrorType errorType) {
            info.ErrorText = errorText;
            info.ErrorType = errorType;
        }

miércoles, 27 de febrero de 2013

Ventana Popup En Control WPF

Para asignar una ventana popup a un control, se puede realizar la siguiente lógica. En nuestro ejemplo vamos a mostrar una ventana popup para un boton, osea al dar clic en el botón se mostrara  la ventana.
El código para mostrar la ventana se agrega en el evento clic del botón:


private void Boton_Click(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            try
            {
                Dictionary<string, string> paramUserControl = new Dictionary<string, string>();
                paramUserControl.Add("unParametro");

                //Instanciamos el control wpf que va a mostrar la información.
                UCExample u = new UCExample ();
                u.SetContext(paramStateTreatment);

                //Instanciamos la ventana contenedora del control
                Popup popup = new Popup()
                {
                    Child = u,
                    AllowsTransparency = true,
                    StaysOpen = true,
                    IsOpen = false,
                    PlacementTarget = ((System.Windows.Controls.Control)(e.OriginalSource)),
                    Placement = PlacementMode.Left
                };

                //Binding para setear la propiedad "Width" del contenedor con la ventana de la información
                Binding widthBinding = new Binding("Width") { Mode = BindingMode.OneWay, Source = popup };
                ((FrameworkElement)u).SetBinding(WidthProperty, widthBinding);

                 //Le damos un tiempo para mostrar la ventana
                DispatcherTimer activeTimer = new DispatcherTimer { Interval = new TimeSpan(0,0,7) };
                activeTimer.Tick += (obj, ea) => ClosePopup(popup);
                activeTimer.Start();

                popup.IsOpen = true;
                return;
            }
            catch (Exception)
            {
                throw;
            }
        }

Dentro de una ventana popup puede ir cualquier template. El código del usercontrol que va a ir dentro de la ventana popup, es el siguiente:

XAML

<UserControl x:Class="Servinte.Clinic.Controls.UCSeeTreatmentStatus"
             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" Height="Auto" Width="Auto">
    <UserControl.Resources>
        <Style TargetType="Label" x:Key="lblControl">
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
        </Style>
        <Style TargetType="TextBlock" x:Key="txtbControl">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>
    <Border Background="White" BorderBrush="Black" BorderThickness="2">
        <Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Nombre " Style="{StaticResource lblControl}"/>
            <TextBlock Grid.Column="1" Name="txtbName" Style="{StaticResource txtbControl}"/>
            <Label Grid.Row="1" Content="Apellidos " Style="{StaticResource lblControl}"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Name="txtbLastName" Style="{StaticResource txtbControl}"/>
            <Label Grid.Row="2" Content="Profesión " Style="{StaticResource lblControl}"/>
            <TextBlock Grid.Row="2" Grid.Column="1" Name="txtbProfession" Style="{StaticResource txtbControl}"/>
            <Label Grid.Row="3" Content="Fecha Nacimiento " Style="{StaticResource lblControl}"/>
            <TextBlock Grid.Row="3" Grid.Column="1" Name="txtbDateBorn" Style="{StaticResource txtbControl}"/>
        </Grid>
    </Border>
</UserControl>


C#

using System.Collections.Generic;
using System.Windows.Controls;

namespace MyNamespace.Controls
{
    /// <summary>
    /// Interaction logic for UCExample.xaml
    /// </summary>
    public partial class UCExample : UserControl
    {
        public UCExample()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Seteamos el contexto de la ventana que informa los datos de la persona
        /// </summary>
        public void SetContext(Dictionary<string,string> param)
        {           
            //Nombre
            txtbName.Text = string.Format(": {0}", param["Name"]);
            //Apellidos
            txtbLastName.Text = string.Format(": {0}", param["LastName"]);
            //Profesion
            txtbProfession.Text = string.Format(": {0}", param["Profesion"]);
            //Fecha de nacimiento
            txtbDateBorn.Text = string.Format(": {0}",param["DateBorn"]));           
        }
    }
}



viernes, 21 de diciembre de 2012

Desactivar Movimiento del Ratón en un combobox

Cuando se selecciona algun valor en un combobox y luego se mueve la rueda del raton teniendo el foco en el combo, se cambia el valor seleccionado anteriormente.
Para desactivar esta función, se desactiva el manejador en el evento PreviewMouseWheel.


private void combobox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
e.Handled = true;
}