Maximus
Maximus

Reputation: 1309

How to access nested properties in XAML?

I currently have the following XAML for my Grid control:

<dxg:GridControl x:Name="DXGrid">
   <dxg:GridControl.Columns>
      <dxg:Column FieldName="Field 1" Width="100"/>
      <dxg:Column FieldName="Field 2" Width="100"/>
      <dxg:Column FieldName="Field 3" Width="100"/>
   </dxg:GridControl.Columns>
<dxg:GridControl>

I would like to move the grid to a UserControl as such:

<UserControl>
   <Grid>
      <dxg:GridControl x:Name="DXGrid"/>
   </Grid>
</UserControl>

and finally, would like to achieve the following:

<Window>
   <Grid>
      <local:MyUserControl>
         <local:MyUserControl.DXGrid.Columns>
            <dxg:Column FieldName="Field 1" Width="100"/>
            <dxg:Column FieldName="Field 2" Width="100"/>
            <dxg:Column FieldName="Field 3" Width="100"/>
         </local:MyUserControl.DXGrid.Columns>
      </local:MyUserControl>
   </Grid>
</Window>

But the problem is that I get an error that states "The attached property DXGrid was not found in the type MyUserControl".

How can I access the DevExpress Grid (nested within a UserControl) and its column collection in XAML?

Upvotes: 2

Views: 2335

Answers (1)

Phil
Phil

Reputation: 153

Short answer - you can't. At least not directly.

However, one way to get around this would be to put a pass-through property on MyUserControl that just relays its information to the nested controls. So it would look something like:

public static readonly DependencyProperty ColumnsProperty =
    DependencyProperty.Register("Columns", typeof(GridColumnCollection), typeof(MyUserControl));

public GridColumnCollection Columns
{
    get { return (GridColumnCollection)GetValue(ColumnsProperty); }
    set { SetValue(ColumnsProperty, value); }
}

Then in your constructor you would set the property to the inner Columns collection:

public MyUserControl()
{
    InitializeComponent();
    Columns = DXGrid.Columns;
}

And with that new property now set up, your XAML will look like this:

<local:MyUserControl>
    <local:MyUserControl.Columns>
        <dxg:Column FieldName="Field1" Width="100"/>
        <dxg:Column FieldName="Field2" Width="100"/>
        <dxg:Column FieldName="Field3" Width="100"/>                
    </local:MyUserControl.Columns>
</local:MyUserControl>

I'm not familiar with DevEx, so I guessed on the name of the GridColumnCollection. Just use whatever type the Columns property is on your DXGrid.

Upvotes: 2

Related Questions