Search This Blog

Wednesday, June 8, 2016

WPF Validation remove red border on error

WPF  > Validation > Remove red border on error


Validation.ErrorTemplate="{x:Null}


Tuesday, June 7, 2016

Clear Validation Error On TextBox TextChanged event WPF

WPF > Validation > ClearInvalid

Removes all ValidationError objects from the specified BindingExpression object.

Example


this.TextChanged += TextBox_TextChanged;

void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
     Validation.ClearInvalid(((TextBox)sender).GetBindingExpression(TextBox.TextProperty));
}


Friday, June 3, 2016

DataGrid WPF show row number

WPF > Controls > ItemsControl > DataGrid > Show row number

XAML


<DataGrid 
        x:Name="dataGrid"
        LoadingRow="dataGrid_LoadingRow"

Code

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
  // show row number
    e.Row.Header = (e.Row.GetIndex() + 1).ToString();

}

Monday, May 23, 2016

DataGridRow change color of selected row

WPF > DataGrid Color of selected row


  <DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFB8AEAE"/>
  </DataGrid.Resources>


Last column width in WPF datagrid to take all the left space

WPF > DataGrid > Last column width


XAML

<DataGridTemplateColumn Header="Range" Width="100'*">

Or Code behind:

 // make the last column in WPF data grid take all the left space, always

dataGrid.Columns[dataGrid.Columns.Count - 1].Width = new DataGridLength(1, DataGridLengthUnitType.Star);


Friday, May 20, 2016

WPF datagrid edit cell on single click

WPF > DataGrid > Edit cell on single click

<Window.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="PreviewMouseLeftButtonDown"    Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        </Style>
    </Window.Resources>

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    EditCell(cell, e);
}

private void EditCell(DataGridCell cell, RoutedEventArgs e)
{
    if (cell == null || cell.IsEditing || cell.IsReadOnly)
       return;

    if (!cell.IsFocused)
    {
                cell.Focus();
    }
    DGrid.BeginEdit(e);
    cell.Dispatcher.Invoke(
           DispatcherPriority.Background,
           new Action(delegate { })
    );

}

Friday, April 8, 2016

PasswordBox WPF Example

WPF > Controls  > PasswordBox 

Represents a control designed for entering and handling passwords.

Example



<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="221" Width="372">
    <Grid Margin="0,0,101,23">
        <PasswordBox x:Name="txtPwd" HorizontalAlignment="Left" Margin="110,75,0,0" VerticalAlignment="Top" Width="120" />
        <TextBlock HorizontalAlignment="Left" Margin="55,47,0,0" TextWrapping="Wrap" Text="User" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="110,47,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <TextBlock HorizontalAlignment="Left" Margin="55,75,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top" RenderTransformOrigin="0.75,2.312"/>
        <Button Content="Login" HorizontalAlignment="Left" Margin="155,116,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
{
   if (txtPwd.Password == "pwd")
     MessageBox.Show("Login Ok");

}