我有一个Snackbar和一个Button,它位于一个位于PopupBox内的StackPanel内。出于某种原因,似乎这种配置不允许直接绑定。我认为它是相关的,因为Snackbar不在VisualTree中,但我完全不了解它以实现我应该如何解决这个问题。
<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>
MainWindow在其构造函数
上分配其ViewModelpublic MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
输出显示以下错误:
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')
和命令在触发时接收参数null。
答案 0 :(得分:1)
修正了这种方式:
<Button CommandParameter="{Binding Source={x:Reference SnackbarOne}}" Command="{Binding CrearAlertaCommand}">