tags:

views:

26

answers:

2

I have defined a custom attached property as follows:

public class DataFilter
{
    public static readonly DependencyProperty FilterColumnProperty =
        DependencyProperty.RegisterAttached("FilterColumn", typeof (string), typeof (DataFilter),
                                            new FrameworkPropertyMetadata(string.Empty,
                                                                          FrameworkPropertyMetadataOptions.Inherits));

    public static void SetFilterColumn(UIElement element, string value)
    {
        element.SetValue(FilterColumnProperty, value);
    }

    public static string GetFilterColumn(UIElement element)
    {
        return (string) element.GetValue(FilterColumnProperty);
    }

}

I am using it in my XAML as follows:

    <Grid Height="600" Name="grdData" cmds:DataFilter.FilterColumn="datasource_id">
      <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
      </Grid.RowDefinitions>
      <igDP:XamDataGrid Name="xamDataGrid1" VerticalAlignment="Top" DataSource="{Binding Tables[Table1]}" ActiveDataItem="{Binding SelectedItem, Mode=OneWayToSource}" cmds:DataFilter.FilterColumn="{Binding FilterCol, Mode=OneWayToSource">

The goal here is to be able to bind the inherited attached property value of cmds:DataFilter.FilterColumn to the VM property of FilterCol. Am not sure how to achieve this. The above code/XAML only sets the default value of the attached property (strng.Empty) to the VM property which is due to the fact that it is creating a new instance of the attached property in the child control. Am not sure what XAML syntax to use to get it to bind to the value set in the Grid (parent) control.

A: 

I had exactly the same issue. And my problem was that the DataContext was not set to the VM but to another object so I just needed to find a way around it and everything works fine now.

rasko
Good to know that you were able to get this to work. Can you expand a little bit more on your solution. Maybe a little code/XAML.
Neerav
A: 

I will try to post some of my code some time later (dont have time now) but by looking again over your code I think your problem is here:

In this line you are setting the DataContext to "{Binding Tables[Table1]}" and I think that is the place where your Binding is trying to find FilterCol and its not there. Try setting {Binding FilterCol, RelativeSource={RelativeSource AncestorType={x:Type Grid}}} hope this helps

rasko