当其他数据绑定正常工作时,命令绑定无法正常工作。

4
我正在使用Jarloo的日历控件,并对其进行了一些更改以满足我的需求。我已经为此苦苦挣扎了很长时间,但我不知道问题可能的原因是什么。
所以,我的问题是我的所有命令绑定都无法工作。
<ListView Background="White"
          SelectionMode="Single"
          dd:DragDrop.DropHandler="{Binding}" 
          dd:DragDrop.IsDropTarget="True"
          dd:DragDrop.UseDefaultDragAdorner="True" 
          ItemsSource="{Binding Typologies}" 
          IsEnabled="{Binding Enabled}">
    <ListView.InputBindings>
        <KeyBinding Key="Delete" Command="{Binding CancelDispatchCommand}" CommandParameter="{Binding SelectedItem}"/>
    </ListView.InputBindings>
....
</ListView>

在这里,按键绑定不起作用,但是{Binding Typologies}和其他数据绑定都很好地工作。这让我认为这不是数据上下文的问题。
<Button DockPanel.Dock="Left" Margin="0,0,10,0" Content="&lt;&lt;" Command="{Binding ChangeDateCmd}" CommandParameter="YEAR,PREV"/>

在这里,我的命令绑定根本不起作用,而这个:

<TextBlock Padding="5" Text="{Binding TargetDate, Converter={StaticResource DateConverter}, ConverterParameter=MONTH}"/>

正常工作...

在 day.cs 中:

private ICommand cancelDispatchCommand;
public ICommand CancelDispatchCommand
{
    get
    {
        return cancelDispatchCommand = cancelDispatchCommand ?? new ActionCommand((o) => CancelDispatch(o));
    }
}

在 calendar.cs 文件中:
ICommand changeDateCmd;
public ICommand ChangeDateCmd
{
    get
    {
        return changeDateCmd = changeDateCmd ?? new ActionCommand((o) => ChangeDate(o));
    }
}

尝试使用RelativeSource的AncestorType ListView,并使用它的DataContext,看看是否有效。 - Maximus
@Maximus 抱歉,我忘了说我已经尝试过了... 这个也不起作用:<KeyBinding Key="Delete" Command="{Binding DataContext.CancelDispatchCommand, RelativeSource={RelativeSource AncestorType=ListView}}" CommandParameter="{Binding SelectedItem}"/> :( - dotixx
输出窗口有任何错误吗? - Maximus
1
@Maximus 嗯,谢谢...显然这个问题是由于无法加载Microsoft.Expressions.Interactions 4.5引起的,所以我不得不将参考版本降级为4.0版本.... - dotixx
1个回答

0

你不需要在命令名中使用 {Binding}。

这里 是一个例子


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接