Search This Blog

Wednesday, July 12, 2017

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>