【问题标题】:WPF ItemsControl Button command binding not workingWPF ItemsControl按钮命令绑定不起作用
【发布时间】:2015-12-09 03:00:51
【问题描述】:

我有一个 ItemsControl 控件,它有一个 ObservableCollection 作为它的 ItemsSource。它还有一个位于其 DataTemplate 内的按钮。按钮的 Command 属性绑定到 ViewModel 中的 RelayCommand(我使用的是 MVVM Light),CommandParameter 绑定到 ItemsSource 中的相应项。
问题是由于某种原因,该命令永远不会触发。另一方面,代码隐藏工作正常。在调试鼠标单击事件处理程序时,我可以看到发送方(Button 类型)有一个填充了正确数据的 CommandParameter,而 Command 为空。

我错过了什么?

XAML:

<ItemsControl ItemsSource="{Binding Users}"
              Margin="{StaticResource ContentMargin}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Margin="{StaticResource ImageButtonMargin}"
                    Style="{StaticResource ImageButtonStyle}"
                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.UserSelectedCommand}"
                    CommandParameter="{Binding}">
                    <!--...-->

视图模型:

private ObservableCollection<User> _users;
private RelayCommand<User> _userSelectedCommand;

public ObservableCollection<User> Users
{
    get { return _users; }
    set
    {
        _users = value;
        RaisePropertyChanged();
    }
}

public RelayCommand<User> UserSelectedCommand
{
    get { return _userSelectedCommand; }
}

protected override sealed void SetCommands() // called in the constructor which is in turned called by SimpleIoc
{
     userSelectedCommand = new RelayCommand<User>((user) => UserSeriesSelected(user));
}

private void UserSelected(User selectedUser)
{

}

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    使用命名元素绑定作为数据模板内的绑定源,以从根数据上下文访问命令。您可以使用根网格或其他容器作为命名元素。 ItemsControl 本身也可以使用。

    <ItemsControl x:Name="MyItems" ItemsSource="{Binding Users}"
                  Margin="{StaticResource ContentMargin}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Margin="{StaticResource ImageButtonMargin}"
                        Style="{StaticResource ImageButtonStyle}"
                        Command="{Binding ElementName=MyItems,  Path=DataContext.UserSelectedCommand}"
                        CommandParameter="{Binding}">
                        <!--...-->
    

    【讨论】:

      【解决方案2】:

      您需要将“FindAncestor”添加到您的相对源绑定中: RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}

      【讨论】:

        【解决方案3】:

        在我看来,你应该改变你的策略,将命令放在User 类中,然后从该类中通过事件通知视图模型。 这将简化您的 xaml 代码,并在我看来,使您的视图模型更加连贯。

        【讨论】:

        • 这是个坏主意。 User,这是一个Model,不应该处理命令。
        • 在这种情况下,只需用视图模型包装用户
        猜你喜欢
        • 2017-01-31
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        相关资源
        最近更新 更多