Search This Blog

Sunday, May 12, 2019

Set current date WPF calendar

WPF -> Calendar

calendar.DisplayDate = new DateTime(2020, 4, 17);
calendar.SelectedDate = new DateTime(2020, 4, 17);


Wednesday, May 8, 2019

WPF Calendar SelectedDates

WPF->Calendar

Add multiple selected dates to WPF Calendar.

<Grid>
   <Calendar x:Name="calendar" />

</Grid>

calendar.SelectionMode = CalendarSelectionMode.MultipleRange; 
calendar.SelectedDates.Add(new DateTime(2020, 1, 1)); 
calendar.SelectedDates.Add(new DateTime(2020, 1, 2));


Wednesday, December 5, 2018

WPF TabControl Example

WPF -> Controls -> TabControl 

WPF TabControl elements display content on discrete pages accessed by selecting the appropriate tab.

Example

<Window x:Class="Agenda.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl>
            <TabItem Header="Agenda">
            </TabItem>
            <TabItem Header="Cards">
            </TabItem>
        </TabControl>
    </Grid>
</Window>



Friday, October 26, 2018

DatePicker WPF Example

System.Windows.Controls -> DatePicker

Represents a control that allows the user to select a date.

Example


<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <DatePicker HorizontalAlignment="Left" Margin="225,118,0,0" VerticalAlignment="Top" SelectedDateChanged="DatePicker_SelectedDateChanged"/>
    </Grid>
</Window>

private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
  var datePicker = sender as DatePicker;
  MessageBox.Show(datePicker.SelectedDate.ToString());
}

Tuesday, August 22, 2017

Display XAML window in the second display WPF

WPF > Windows > Forms > Screen

AllScreens gets an array of all displays on the system.

Example

Display window in the second display WPF

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   var screen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).First();
   var area = screen.WorkingArea;
   Left = area.Left;
   Top = area.Top;
   Width = area.Width;
   Height = area.Height;
}

Wednesday, July 26, 2017

GridViewColumnHeader WPF Example

WPF > Controls  > ContentControl > GridViewColumnHeader

GridViewColumnHeader object is a button control that is derived from ButtonBase. 
Column headers in a GridView view mode are GridViewColumnHeader objects.
Example

<Window x:Class="GridViewColumnHeader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GridViewColumnHeader"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumnHeader>Name
                            <GridViewColumnHeader.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Ascending" />
                                    <MenuItem Header="Descending" />
                                </ContextMenu>
                            </GridViewColumnHeader.ContextMenu>
                        </GridViewColumnHeader>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>



Wednesday, July 12, 2017

Get Vertical ScrollBar Width WPF

WPF > Windows > SystemParameters > VerticalScrollBarWidth


This value indicates the width (in pixels) of a vertical scroll bar.

SystemParameters.VerticalScrollBarWidth

WPF SystemParameters Class

WPF > Windows > SystemParameters


Thursday, May 4, 2017

Bind Width to another Width control WPF

WPF > Binding > Width

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition 
Width="{Binding ElementName=controlName, Path=ActualWidth}"/>
  </Grid.ColumnDefinitions>

Monday, March 13, 2017

ScrollToTop DataGrid WPF

WPF> DataGrid > Scroll To Top


var border = VisualTreeHelper.GetChild(myDataGrid, 0) as Decorator;
if (border != null && border.Child !=null)
{
  var scrollViewer = border.Child as ScrollViewer;
  if (scrollViewer != null)
  {
    scrollViewer.ScrollToTop();
  }

}

Thursday, March 2, 2017

FrameworkElementFactory focus on load element

WPF > FrameworkElementFactory > AddHandler


FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.AddHandler(TextBox.LoadedEvent, new RoutedEventHandler(OnTextBoxLoaded));

private void OnTextBoxLoaded(object sender, RoutedEventArgs e)
{
       TextBox textBox = sender as TextBox;
       if (textBox != null)
       {
              textBox.Focus();
       }
}

Tuesday, February 21, 2017

Disable row selection datagrid WPF


How to disable row selection in datagrid WPF?

Answer:

myDataGrid.IsHitTestVisible = false;

Thursday, January 26, 2017

Find TextBox control ComboBox WPF

WPF > Controls > ItemsControl > ComboBox

Use FindName to find an element that has the provided identifier name.


PART_EditableTextBox
TextBox
Contains the text of the ComboBox

Example

var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

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>