【问题标题】:Command bindings doesn't work when other data bindings works当其他数据绑定有效时,命令绑定无效
【发布时间】:2015-09-17 12:39:15
【问题描述】:

我正在使用Jarloo's calendar control 并在其上添加一些更改以满足我的需求。我已经为此苦苦挣扎了很长时间,但我不知道我的问题可能是什么原因。

所以,我的问题是我的 Command 绑定都不起作用。

<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} 和其他 DATA 绑定都运行良好。这让我认为这不是数据上下文问题。

<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 对不起,我忘了说我已经试过了......这也不好用:&lt;KeyBinding Key="Delete" Command="{Binding DataContext.CancelDispatchCommand, RelativeSource={RelativeSource AncestorType=ListView}}" CommandParameter="{Binding SelectedItem}"/&gt; :(
  • 输出窗口有错误吗?
  • @Maximus 好吧,谢谢...显然问题出在它无法加载 Microsoft.Expressions.Interactions 4.5 所以我不得不降级对 4.0 版本的引用 ....

标签: c# wpf data-binding binding wpf-controls


【解决方案1】:

您不需要将 {Binding } 与命令名称一起使用。

Here 是一个例子

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 2012-02-11
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2010-10-13
    相关资源
    最近更新 更多