Question

I have to pass a CommandParameter of the GridControl for serialization of the layout.

My button to execute the command is in a child usercontrol.

I am successfully using RelativeSource to get to the Grid that contains the GridControl.

Edit: This button is in a usercontrol named GridSettings.xaml. This is a child of Grid.xaml.

<Button Content="Save Defaults" Command="{Binding SaveDefaultsCommand}" Width="90" CommandParameter="{Binding Path=gridControl1, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=2}}"/>

Here is the pertinent section of the Grid.xaml parent view.

                <Grid Grid.Row="1" x:Name="GridView">
                <dxg:GridControl x:Name="gridControl1"  ItemsSource="{Binding WeldReports}" AutoPopulateColumns="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Loaded">
                            <Custom:EventToCommand Command="{Binding GridLoadedCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <dxg:GridControl.View>
                        <dxg:TableView Name="tableView1" ShowTotalSummary="True" />
                    </dxg:GridControl.View>
                </dxg:GridControl>
            </Grid>

Error I get: System.Windows.Data Error: 40 : BindingExpression path error: 'gridControl1' property not found on 'object' ''Grid' (Name='GridView')'. BindingExpression:Path=gridControl1; DataItem='Grid' (Name='GridView'); target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object')

Path=gridControl1 should really be ElementName=gridControl1...but ElementName does not work with RelativeSource...so I read.

Was it helpful?

Solution

The Path in a Binding needs to represent a property. You can create this for your GridControl by adding a wrapper property to Grid.xaml.cs that just returns the gridControl1 field already created for you because of the x:Name set in XAML.

public object MyGridControl
{
  get { return gridControl1; }
}

CommandParameter="{Binding Path=MyGridControl, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=2}}"

You should also consider trying to restructure your application to avoid needing to pass a UI control as a command parameter, especially needing to go outside the local scope as you're doing here. The way it is set up now the controls are very tightly coupled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top