I want to specify attached property for Binding.

For instance, I want to know the change in the Grid.Row property in the MyGrid
class. It is the following codes that I tried.

public class MyGrid : Grid
{
static MyGrid()
{
FrameworkPropertyMetadata metaData =
new FrameworkPropertyMetadata(0);
metaData.PropertyChangedCallback += OnAttachedRowChanged;
Grid.RowProperty.OverrideMetadata( typeof(MyGrid), metaData );
}
private static void OnAttachedRowChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// It is not called.
MessageBox.Show( "Call OnAttachedRowChanged" );
}




Answer this question

I want to specify attached property for Binding.

  • Nipam N. Patel

    It is not possible to straighten out that problem by "Grid.RowProperty.OverrideMetadata".
    Please teach the method of receiving the notification.


  • Pavan Apuroop

    I'm not sure why that OverrideMetadata isn't working, but another thing you could try is to override the OnPropertyChanged method of Grid. In there, you can look for and handle the case where the RowProperty has changed. (Just make sure you also call base.OnPropertyChanged from your override.)


  • Leonard51607

    you may have to specify xmlnamespace of also. It is not able to find 'ColorComboBox'

  • Amos Soma

    try

    <SolidColorBrush Color="{Binding Path=(ColorComboBox.ItemColor), RelativeSource={RelativeSource Mode=TemplatedParent}}"/>



  • Derek Smyth

    Thank you.
    It tried immediately.

    However, the exception has been generated.

    Cannot convert string '(ColorComboBox.ItemColor)' in attribute 'Path' to object of type 'System.Windows.PropertyPath'. Property path is not valid. Cannot resolve type name 'ColorComboBox'. Error at object 'System.Windows.Data.Binding'.

    By the way, I am using June CTP.



  • progames25

    Thank you.
    It operated by specifying xmlnamespace.

    <SolidColorBrush Color="{Binding Path=(local:ColorComboBox.ItemColor), RelativeSource={RelativeSource Mode=TemplatedParent}}"/>



  • The Philosiphiser

    It is not referred in the following codes though I want to relate to attached
    property with Binding.

    System.Windows.Data Error: 35 : BindingExpression path error: 'ColorComboBox' property not found on 'object' ''ComboBoxItem' (Name='')'. BindingExpression:Path=ColorComboBox.ItemColor; DataItem='ComboBoxItem' (Name=''); target element is 'SolidColorBrush' (HashCode=60790550); target property is 'Color' (type 'Color')

    public class ColorComboBox : ComboBox
    {
    public static readonly DependencyProperty ItemColorProperty =
    DependencyProperty.RegisterAttached("ItemColor", typeof(Color), typeof(ColorComboBox),
    new UIPropertyMetadata(Colors.White));
    public static Color GetItemColor(DependencyObject obj)
    {
    return (Color)obj.GetValue(ItemColorProperty);
    }

    public static void SetItemColor(DependencyObject obj, Color value)
    {
    obj.SetValue(ItemColorProperty, value);
    }
    }

    <Style
    x:Key="ColorComboBoxItemStyle"
    TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
    <Border Padding="2" BorderBrush="Pink" BorderThickness="1"
    ToolTip="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}">
    <Rectangle MinWidth="8" MinHeight="8">
    <Rectangle.Fill>
    <SolidColorBrush Color="{Binding Path=ColorComboBox.ItemColor, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
    </Rectangle.Fill>
    </Rectangle>
    </Border>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>



  • figuerres

    I want to learn the change of child's Grid.Row with MyGird.
    However, it is not possible to achieve it by the method.

    Code :
     public partial class Window1 : Window
     {

      public Window1()
      {
       InitializeComponent();
      }

      void OnBtnClick(object sender, RoutedEventArgs e)
      {
       System.Diagnostics.Debug.WriteLine( "OnBtnClick Start! ");
       UIElement dep = (UIElement)sender;
       Grid.SetColumn( dep, Grid.GetColumn(dep) + 1 );

       System.Diagnostics.Debug.WriteLine( "OnBtnClick End! ");
      }
     }

     public class MyGrid : Grid
     {
      protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
      {
       System.Diagnostics.Debug.WriteLine( "MyGrid.OnPropertyChanged : " + e.Property.ToString() );
       base.OnPropertyChanged(e);
      }
     }

     <Window x:Class="WindowsApplication3.Window1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:l="clr-namespace:WindowsApplication3"
         Title="WindowsApplication3" Height="300" Width="300"
         >
      <l:MyGrid>
       <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
       </Grid.RowDefinitions>
       <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition Width="50"/>
       </Grid.ColumnDefinitions>
       <Button Click="OnBtnClick" Grid.Row="0"></Button>
       <Button Click="OnBtnClick" Grid.Row="1"></Button>
      </l:MyGrid>
     </Window>

    Result : The change in the Grid.Column property cannot be acquired.
     OnPropertyChanged : XmlnsDictionary
     OnPropertyChanged : IWindowService
     OnPropertyChanged : Foreground
     OnPropertyChanged : XmlNamespaceMaps
     OnPropertyChanged : IsVisible
     OnPropertyChanged : ActualWidth
     OnPropertyChanged : ActualHeight
     OnPropertyChanged : ShowKeyboardCues
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsKeyboardFocusWithin
     OnPropertyChanged : IsMouseCaptureWithin
     OnPropertyChanged : IsMouseCaptureWithin
     OnBtnClick Start!
     OnBtnClick End!
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseCaptureWithin
     OnPropertyChanged : IsMouseCaptureWithin
     OnBtnClick Start!
     OnBtnClick End!
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsMouseCaptureWithin
     OnPropertyChanged : IsMouseCaptureWithin
     OnBtnClick Start!
     OnBtnClick End!
     OnPropertyChanged : IsMouseOver
     OnPropertyChanged : IsVisible
     OnPropertyChanged : IsKeyboardFocusWithin



  • I want to specify attached property for Binding.