Search This Blog

Wednesday, December 14, 2016

Set DataGrid DataGridRow Background color WPF

WPF > DataGrid > DataGridRow > Background


<DataGrid.RowStyle>
  <Style TargetType="DataGridRow">
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsValid}" Value="true">
        <Setter Property="Background" Value="Green"></Setter>
      </DataTrigger>
      <DataTrigger Binding="{Binding IsValid}" Value="false">
        <Setter Property="Background" Value="Red"></Setter>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</DataGrid.RowStyle>

Wednesday, November 16, 2016

Clear Sort WPF DataGrid

WPF > Controls > ItemsControl > DataGrid > Clear Sort

_collectionView = (CollectionView)CollectionViewSource.GetDefaultView(dataGrid.Items);

_collectionView.SortDescriptions.Clear();

Thursday, November 3, 2016

Convert Hex to SolidColorBrush

WPF > System.Windows.Media > BrushConverter

BrushConverter is used to convert a Brush object to or from another object type.

Example:

row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFA7CDF0"));

Monday, October 24, 2016

Get Current Row WPF DataGrid

WPF > Controls > ItemsControl > DataGrid > ItemContainerGenerator > ContainerFromIndex

Returns the element corresponding to the item at the given index.

Example

How to get current row if row is not selected.

var index = dataGrid.Items.IndexOf(dataGrid.CurrentItem);


var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);




Wednesday, October 19, 2016

Get rectangles from canvas WPF code behind

WPF > Controls > Layout > Canvas > Children


Example 

Get all rectangles from canvas.

private List<Rectangle> GetRectangles()
{
    return canvas.Children.OfType<Rectangle>().ToList();

}

Thursday, October 13, 2016

Round line WPF code behind

WPF > Line 


Line line = new Line
{
 Stroke = Brushes.Blue,
 StrokeThickness = 18,
 StrokeStartLineCap = PenLineCap.Round,
 StrokeEndLineCap = PenLineCap.Round,
 Height= 50,
 X1 = 50,
 X2 = 100,
 Y1 = 10,
 Y2 = 10
};
canvas.Children.Add(line);

        

Thursday, September 29, 2016

Slider WPF Example

WPF > Controls > Slider 

    <Grid>
        <TextBox
            Text="{Binding ElementName=Slider, Path=Value,             UpdateSourceTrigger=PropertyChanged}"
            Margin="33,112,288,172"/>
        <Slider
            Maximum="100"  
            Name="Slider"
            Margin="33,48,288,213"
            IsSnapToTickEnabled="True"
            TickFrequency="1" />
    </Grid>

Monday, August 22, 2016

Binding ComboBox to Enum and use Tag as SelectedValuePath

WPF > Controls > ItemsControl > ComboBox > Bind ComboBox to Enum

XAML


<Window x:Class="ComboBoxTag.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox
                  SelectedValue="{Binding Rate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  SelectedValuePath="Tag" Margin="0,0,306,295">
            <!-- Tag is values of Rate enum -->
            <ComboBoxItem Tag="Bad">Bad</ComboBoxItem>
            <ComboBoxItem Tag="Low">Low</ComboBoxItem>
        </ComboBox>
    </Grid>

</Window>

C#

namespace ComboBoxTag
{
    public enum Rate
    {
        Bad = 0,
        Good = 1
    }
}

Thursday, July 28, 2016

GridSplitter WPF Example

WPF > Controls  > GridSplitter 




<Window x:Class="GridSplitter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock1" VerticalAlignment="Top" Grid.Column="0"/>
        <GridSplitter Grid.Column="1" Width="2" HorizontalAlignment="Stretch" />
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock2" VerticalAlignment="Top" Grid.Column="2"/>
    </Grid>
</Window>


Wednesday, July 6, 2016

TextBlock WPF Example

WPF > Controls  > TextBlock 

TextBlock can contain a string in its Text property.

TextBlock is designed to be lightweight and is optimized for single-line display.

Example


<Window x:Class="TextBlock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Name="textBlock1" TextWrapping="Wrap">
            Simple text <Bold>Bold text</Bold> <Italic>Italic text</Italic>
        </TextBlock>
    </Grid>

</Window>

WPF Rectangle with Text

WPF > Rectangle > Add Text



<Window x:Class="Rectangle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid  Height="22">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Fill="Aqua"
                   Grid.Column="1"
                   HorizontalAlignment="Stretch"
                   />
        <TextBlock 
                   Foreground="Blue"
                   Grid.Column="1"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Text="Rectangle Text"
                   TextTrimming="CharacterEllipsis" />
    </Grid>
</Window>

Tuesday, July 5, 2016

WPF DependencyProperty Register Default Value

WPF > DependencyPropertyRegister > Default Value



public static readonly DependencyProperty
UpdateSourceTriggerProperty = DependencyProperty.Register("UpdateSourceTrigger", typeof(UpdateSourceTrigger),
typeof(TextBox),
new FrameworkPropertyMetadata(UpdateSourceTrigger.LostFocus)); // add your default value here


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>