Posts Tagged ‘ attached properties ’

[WPF] Set Attached Properties (Grid.Column, Grid.Row, Canvas.Top, Canvas.Left, etc.) in C# Code

Hello everybody,

if you tried to create a Grid, Canvas or a DockPanel in C# problem than you probably had the problem that you do not know how to add the children to a specific column, row or whatever.

These properties are so called Attached Properties. And you can not see them if you call textBox.Grid.Row in the C# code.

Here an example

   <TextBox Grid.Row="0" Grid.Column="0" />

But you can not do the following in your C# Code

<pre>Grid grid = new Grid();
TextBox txt = new TextBox();
txt.Grid.Column = 1; //Not possible!

But you can do the following

//Set the Grid.Row Attached Property of the TextBox txt to 0
Grid.SetRow(txt, 0);
//Set the Grid.Column Attached Property of the TextBox txt to 0

It works in the same way for all other Attached Properties

ListBox – Automatically scroll to bottom

In WinForms it was so simple to tell the ListBox to scroll to the buttom.
But in WPF it seems to be rather difficult but it is not.
You can simply create an attached property and once done it will do it for you.

Simply add the attached property to your ListBox

<ListBox ItemsSource="{....}" Extenders:ListBoxExtenders.AutoScrollToEnd="True" />

And here is the source code of the attached property

/// <summary>
  /// This class contains a few useful extenders for the ListBox
  /// </summary>
  public class ListBoxExtenders : DependencyObject
    public static readonly DependencyProperty AutoScrollToEndProperty = DependencyProperty.RegisterAttached("AutoScrollToEnd", typeof(bool), typeof(ListBoxExtenders), new UIPropertyMetadata(default(bool), OnAutoScrollToEndChanged));

    /// <summary>
    /// Returns the value of the AutoScrollToEndProperty
    /// </summary>
    /// <param name="obj">The dependency-object whichs value should be returned</param>
    /// <returns>The value of the given property</returns>
    public static bool GetAutoScrollToEnd(DependencyObject obj)
      return (bool)obj.GetValue(AutoScrollToEndProperty);

    /// <summary>
    /// Sets the value of the AutoScrollToEndProperty
    /// </summary>
    /// <param name="obj">The dependency-object whichs value should be set</param>
    /// <param name="value">The value which should be assigned to the AutoScrollToEndProperty</param>
    public static void SetAutoScrollToEnd(DependencyObject obj, bool value)
      obj.SetValue(AutoScrollToEndProperty, value);

    /// <summary>
    /// This method will be called when the AutoScrollToEnd
    /// property was changed
    /// </summary>
    /// <param name="s">The sender (the ListBox)</param>
    /// <param name="e">Some additional information</param>
    public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
      var listBox = s as ListBox;
      var listBoxItems = listBox.Items;
      var data = listBoxItems.SourceCollection as INotifyCollectionChanged;

      var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
          (s1, e1) =>
            if (listBox.Items.Count > 0)
            object lastItem = listBox.Items[listBox.Items.Count - 1];

      if ((bool)e.NewValue)
        data.CollectionChanged += scrollToEndHandler;
        data.CollectionChanged -= scrollToEndHandler;

Note that it is similar to the scroll current element into view
So check this out: scroll current item into view