Vallo
Vallo

Reputation: 1967

Binding fails for CommandParameter - Cannot find source for binding with reference 'ElementName=SnackbarOne'

I have a Snackbar and a Button which is inside a StackPanel which is inside a PopupBox. For some reason it seems this configuration does not allow direct binding. I think it's related because the Snackbar is not in the VisualTree but I don't understand it completely to realize how I should fix this.

<controls:MetroWindow x:Class="OmniClient.MainWindow" >
    <Grid>
        <materialDesign:PopupBox Grid.Column="1" Grid.Row="1"
               Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}" 
               PlacementMode="TopAndAlignCentres" ToolTipService.Placement="Right" PopupMode="MouseOver">
        <StackPanel>
            <Button ToolTip="Nueva alerta" CommandParameter="{Binding ElementName=SnackbarOne}"
                    Command="{Binding CrearAlertaCommand}" >
                <materialDesign:PackIcon Kind="AlarmPlus" />
            </Button>
        </StackPanel>
        </materialDesign:PopupBox>
        <materialDesign:Snackbar Grid.Column="0" Grid.Row="1"  Width="auto" Grid.ColumnSpan="2" MessageQueue="{materialDesign:MessageQueue}" x:Name="SnackbarOne" />
    </Grid>
</MetroWindow>

The MainWindow assigns its ViewModel on its constructor

public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
    }

The output shows the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=SnackbarOne'. BindingExpression:Path=IsActive; DataItem=null; target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object')

and command receives parameter null when it's triggered.

Upvotes: 0

Views: 990

Answers (1)

Vallo
Vallo

Reputation: 1967

Fixed this way:

<Button CommandParameter="{Binding Source={x:Reference SnackbarOne}}" Command="{Binding CrearAlertaCommand}">

Upvotes: 1

Related Questions