Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How remove focus on edit cell after click reset button

$
0
0

First, try to input invalid value in datagrid cell, then click reset button. After that you will see although values are being reset however the focus still on the datagrid cell and still blocking to edit other cells within the same grid, how to solve this problem ? Thanks.


Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"><ControlTemplate x:Key="ValidationErrorTemplate"><DockPanel><Image x:Name="imgValidation" ToolTip="{Binding ElementName=controlWithError,
                    Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                    Source="/ValidationInDataGrid;component/Resources/invalid.png" Width="16" Height="16" /><AdornedElementPlaceholder x:Name="controlWithError"  Margin="-2, 0, 0, 0"/></DockPanel></ControlTemplate><Style x:Key="baseDatagridCell" TargetType="{x:Type tk:DataGridCell}"><Setter Property="VerticalContentAlignment" Value="Center"/><Setter Property="VerticalAlignment" Value="Stretch"/><Style.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition  Property="IsFocused" Value="True"/><Condition  Property="IsSelected" Value="True"/></MultiTrigger.Conditions><Setter Property="Background" Value="#F1F2F4"/><Setter Property="BorderBrush" Value="#77BBFF"/><Setter Property="BorderThickness" Value="2"/><Setter Property="Background" Value="#F1F2F4"/></MultiTrigger><MultiTrigger><MultiTrigger.Conditions><Condition  Property="IsFocused" Value="False"/><Condition  Property="IsSelected" Value="True"/></MultiTrigger.Conditions><Setter Property="Background" Value="{x:Null}"/></MultiTrigger><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsMouseOver" Value="True"/><Condition  Property="IsFocused" Value="True"/></MultiTrigger.Conditions><Setter Property="Background" Value="#FFFFFF"/><Setter Property="BorderBrush" Value="#3399FF"/><Setter Property="BorderThickness" Value="2"/></MultiTrigger><Trigger Property="IsEditing" Value="True"><Setter Property="Background" Value="#FFFFFF"/><Setter Property="BorderBrush" Value="#3399FF"/><Setter Property="BorderThickness" Value="1"/><Setter Property="Foreground" Value="#BBDDFF"/></Trigger></Style.Triggers></Style><Style x:Key="testPercent" TargetType="{x:Type TextBlock}"><Setter Property="TextAlignment" Value="Right"/><Setter Property="Width" Value="92" /><Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTemplate}"/><Setter Property="Foreground" Value="#000000" /></Style><Style x:Key="testPercentEditing" TargetType="{x:Type TextBox}"><Setter  Property="TextAlignment" Value="Right"/><Setter Property="HorizontalAlignment" Value="Stretch" /><Setter Property="Width" Value="92" /><Setter Property="Height" Value="20"/><Setter Property="Padding" Value="-3"/><Setter Property="Validation.ErrorTemplate"><Setter.Value><ControlTemplate><DockPanel Height="18"><Image Height="18" x:Name="imgValidation" ToolTip="{Binding ElementName=controlWithError,
                            Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                            Source="/ValidationInDataGrid;component/Resources/invalid.png"/><AdornedElementPlaceholder x:Name="controlWithError"  Margin="-15, 0, 0, 0"/></DockPanel></ControlTemplate></Setter.Value></Setter><Setter Property="MaxLength" Value="11" /></Style><Style TargetType="{x:Type tk:DataGrid}" ><Setter Property="AutoGenerateColumns" Value="False"/><Setter Property="CanUserAddRows" Value="False"/><Setter Property="CanUserDeleteRows" Value="False"/><Setter Property="CanUserReorderColumns" Value="False"/><Setter Property="CanUserResizeColumns" Value="False"/><Setter Property="CanUserResizeRows" Value="False"/><Setter Property="CanUserSortColumns" Value="False"/><Setter Property="AlternationCount" Value="2"/><Setter Property="CellStyle" Value="{StaticResource baseDatagridCell}"/><Setter Property="RowHeight" Value="24"/><Setter Property="SelectionUnit" Value="CellOrRowHeader"/><Setter Property="SelectionMode" Value="Single"/><Setter Property="BorderThickness" Value="1"/></Style></ResourceDictionary>


MainWindowView.xaml

<Window x:Class="ValidationInDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:view="clr-namespace:ValidationInDataGrid.Views"
        Title="MainWindow" Height="350" Width="525"><StackPanel><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><view:DataGridView Grid.Column="0" DataContext="{Binding Percentage1}" BorderThickness="5, 5, 0 , 5"  /><view:DataGridView Grid.Column="1"  DataContext="{Binding Percentage2}" BorderThickness="0, 5, 5, 5" /></Grid><Button Grid.Column="3" Command="{Binding Reset}" Content="Reset" /></StackPanel></Window>

DataGrid.xaml

<UserControl x:Class="ValidationInDataGrid.Views.DataGridView"
             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"
             xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
             d:DesignHeight="300" d:DesignWidth="300"><UserControl.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/ValidationInDataGrid;component/Generic.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></UserControl.Resources><tk:DataGrid ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" RowHeaderWidth="0" IsTabStop="False" BorderThickness="0"><tk:DataGrid.Columns><tk:DataGridTextColumn Header="Percentage" Binding="{Binding PercentValue, StringFormat={}{0:0.########}, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                EditingElementStyle="{StaticResource testPercentEditing}"
                ElementStyle="{StaticResource testPercent}"></tk:DataGridTextColumn></tk:DataGrid.Columns></tk:DataGrid></UserControl>

PercentageViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using ValidationInDataGrid.Models;

namespace ValidationInDataGrid.ViewModels
{
    public class PercentageViewModel : ViewModelBase, IDataErrorInfo
    {
        private decimal percentValue;
        private string error;

        public decimal PercentValue
        {
            get
            {
                return percentValue;
            }
            set
            {
                percentValue = value;
                OnPropertyChanged("PercentValue");
            }
        }

        public PercentageViewModel()
        {
        }

        public PercentageViewModel(PercentageItemInfo percentageItemInfo)
        {
            if (percentageItemInfo == null)
            {
                throw new ArgumentNullException("percentageItemInfo");
            }

            this.percentValue = percentageItemInfo.PercentValue;
        }

        public string Error
        {
            get { return this.error; }
        }

        public string this[string propertyName]
        {
            get { return PerformValidation(propertyName); }
        }

        private string PerformValidation(string propertyName)
        {
            error = null;

            //testing validation

            switch (propertyName)
            {
                case "PercentValue":
                    if (this.PercentValue < 0 || this.PercentValue > 10)
                    {
                        this.error = string.Format(CultureInfo.CurrentCulture, "Error !");
                    }
                    break;
                default:
                    break;
            }

            return error;
        }
    }
}


Download Project



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>