Wpf Binding Button.IsEnabled到CollectionView中的当前位置

Wpf Binding Button.IsEnabled到CollectionView中的当前位置,wpf,binding,collectionviewsource,Wpf,Binding,Collectionviewsource,我正在尝试将按钮的IsEnabled属性绑定到窗口的CollectionViewSource属性。我这样做是为了实现First/Previous/Next/Last按钮,并希望在视图位于第一项上时禁用First和Previous等 我已经设置了集合视图源代码,UI控件正确绑定到它,并且可以在代码中访问它的视图,因此单击事件处理程序在视图中导航时可以正常工作 <CollectionViewSource x:Key="cvMain" /> 我已尝试此操作,但出现绑定错误“B

我正在尝试将按钮的IsEnabled属性绑定到窗口的CollectionViewSource属性。我这样做是为了实现First/Previous/Next/Last按钮,并希望在视图位于第一项上时禁用First和Previous等

我已经设置了集合视图源代码,UI控件正确绑定到它,并且可以在代码中访问它的视图,因此单击事件处理程序在视图中导航时可以正常工作

      <CollectionViewSource x:Key="cvMain" />
我已尝试此操作,但出现绑定错误“BindingExpression路径错误:“”在“对象”“ListCollectionView”“上找不到属性”


我试图先使用转换器,但我认为使用触发器的样式会更有效,但无法访问集合视图。即使将基础datacontext设置为集合视图源,绑定也会作为视图的源传递给转换器(如果我没有明确设置绑定的源,如上所述),该转换器没有货币属性(CurrentPosition、Count等)


非常感谢您的帮助。

为什么不为此使用
RoutedCommand
(即使您没有使用MVVM)

比如说:

<Button x:Name="nextButton"
        Command="{x:Static local:MainWindow.nextButtonCommand}"
        Content="Next Button" />
您还可以应用中的建议之一手动重新评估
按钮。isEnabled
状态


这样可以将与按钮相关的封装逻辑放在一个区域。

尝试将源属性更改为ElementName。可能会有帮助。您在哪里显示数据
DataGrid
或…我不明白如何使用ElementName,因为集合视图是在窗口的资源中声明的,而不是作为可视化树的元素。我正在DataGrid中显示列表数据,并带有显示当前项属性的其他文本框,所有这些都与视图中的导航同步工作。谢谢Viv,这非常有效。我使用内置命令“LastPage”和“NextPage”搭车,而不是创建自定义命令“nextButtonCommand”(尽管这可能需要更改)。无需执行任何操作即可使CanExecuteChanged()在按钮启用状态下工作。
private void Window_Loaded(object sender, RoutedEventArgs e) {
  ((CollectionViewSource)Resources["cvMain"]).Source = FoJobs;
  cvJobs = (CollectionView)((CollectionViewSource)Resources["cvMain"]).View;
}
        <Button Name="cbFirst" Click="cbMove_Click" IsEnabled="{Binding Source={StaticResource cvMain}, Converter={StaticResource CurrPos2BoolConverter}}" />
<Button x:Name="nextButton"
        Command="{x:Static local:MainWindow.nextButtonCommand}"
        Content="Next Button" />
public static RoutedCommand nextButtonCommand = new RoutedCommand();

public MainWindow() {
  InitializeComponent();
  CommandBinding customCommandBinding = new CommandBinding(
    nextButtonCommand, ExecuteNextButton, CanExecuteNextButton);
  nextButton.CommandBindings.Add(customCommandBinding);  // You can attach it to a top level element if you wish say the window itself
}

private void CanExecuteNextButton(object sender, CanExecuteRoutedEventArgs e) {
  e.CanExecute = /* Set to true or false based on if you want button enabled or not */
}

private void ExecuteNextButton(object sender, ExecutedRoutedEventArgs e) {
  /* Move code from your next button click handler in here */
}