Monday, October 10, 2016

SharedSizeGroup in WPF

SharedSizeGroup in WPF

Share same column or row size across multiple grids in wpf.

<Grid Grid.IsSharedSizeScope="True">
   <Grid>
       <Grid.ColumnDefinitions>
           <ColumnDefinition Width="Auto" SharedSizeGroup="Name"/>
           <ColumnDefinition Width="Auto" SharedSizeGroup="Value"/>
       </Grid.ColumnDefinitions>
      <Label Grid.Column=0/>
      <Label Grid.Column=1/>
   </Grid>
   <Grid>
       <Grid.ColumnDefinitions>
           <ColumnDefinition Width="Auto" SharedSizeGroup="Name"/>
           <ColumnDefinition Width="Auto" SharedSizeGroup="Value"/>
       </Grid.ColumnDefinitions>
      <Label Grid.Column=0/>
      <Label Grid.Column=1/>
   </Grid>
</Grid>


The size of columns of both grid is shared each other even that is set through dynamically.

Listen Dependency Property is Changed without event in WPF

Listen Dependency Property is Changed without event in WPF

You wanna listen any dependency property of any object is changed without event or which does not have event,  here we go,

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBox.IsFocusedProperty,  typeof(TextBox));

dpd.AddValueChanged(txtName, (s, e)=>
{
     if(txtName.IsFocused)
                MessageBox.Show("Focused");

});