【问题标题】:Command Parameter is null in a Command of CustomDataGridCustomDataGrid 的命令中的命令参数为空
【发布时间】:2010-09-16 06:52:57
【问题描述】:

这段代码运行良好:

<sdk:DataGrid ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem,Mode=TwoWay}" x:Name="dataGrid">
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CommandName}" CommandParameter="{Binding ElementName=MainTabControl,Mode=OneWay}"/>
            </i:EventTrigger>
    </i:Interaction.Triggers>

在我的 CustomDataGrid 中,命令执行但 CommandParameter 为空:

<customControl:CustomDataGrid ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem,Mode=TwoWay}" x:Name="dataGrid">
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CommandName}" CommandParameter="{Binding ElementName=MainTabControl,Mode=OneWay}"/>
            </i:EventTrigger>
    </i:Interaction.Triggers>

编辑: 我的CustomDataGrid 是从标准DataGrid 驱动的,并向其中添加一个事件。它overrides 两个event:OnLoadingRow 和OnUnLoadingRow。

注意一个简单的CommandParameter 例如一个简单的字符串发送正常。

而 MainTabControl 是:

<sdk:TabControl Name="MainTabControl"> ...

有什么问题?

【问题讨论】:

  • 您在运行时是否遇到绑定错误?检查你的输出窗口
  • 不,没有绑定错误。

标签: silverlight xaml mvvm datagrid command


【解决方案1】:

好的,我已经为您的情况构建了一个完整的模型,并在下面提供了所有代码。一切都是准系统,所以请大家减轻我在其中的不良做法的数量:)

坏消息是,我的派生类工作方式与普通 DataGrid 完全相同!当命令断点被派生网格命中时,参数是完全相同的 TabControl 类型。

为了更进一步,我需要看看你可能在做什么(除了你已经提到的)。如果您不想公开您的代码,请随时通过我的网站与我联系。

查看:

<UserControl xmlns:my="clr-namespace:SilverlightApplication4"  xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
    x:Class="SilverlightApplication4.Page"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="59*" />
            <RowDefinition Height="241*" />
        </Grid.RowDefinitions>
        <sdk:TabControl x:Name="MainTabControl">
            <sdk:TabItem Header="Tab1">Tab 1 content</sdk:TabItem>
            <sdk:TabItem Header="Tab2">Tab 2 content</sdk:TabItem>
            <sdk:TabItem Header="Tab3">Tab 3 content</sdk:TabItem>
        </sdk:TabControl>
        <!--data:DataGrid Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AutoGenerateColumns="True" ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" x:Name="dataGrid" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CommandName}" CommandParameter="{Binding ElementName=MainTabControl,Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </data:DataGrid-->
        <my:CustomDataGrid Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AutoGenerateColumns="True" ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" x:Name="dataGrid2" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CommandName}" CommandParameter="{Binding ElementName=MainTabControl,Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </my:CustomDataGrid>
    </Grid>
</UserControl>

代码隐藏:

using System.Windows.Controls;

namespace SilverlightApplication4
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }
}

视图模型:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;

namespace SilverlightApplication4
{
    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand CommandName { get; set; }
        public ObservableCollection<Item> MyItems { get; set; }

        public Item MySelectedItem { get; set; }

        public ViewModel()
        {
            MyItems = new ObservableCollection<Item>();
            MyItems.Add(new Item() { FirstName = "Joe", LastName = "Blogs" });
            MyItems.Add(new Item() { FirstName = "Another", LastName = "One" });
            MyItems.Add(new Item() { FirstName = "Jane", LastName = "Blogs" });
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyItems"));
            }
            CommandName = new DelegateCommand<TabControl>(OnCommandAction);
        }

        public void OnCommandAction(TabControl param)
        {
            // Breakpoint here
        }

    }

    public class Item
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

自定义数据网格:

using System.Windows.Controls;

namespace SilverlightApplication4
{
    public class CustomDataGrid : DataGrid
    {
        protected override void OnLoadingRow(DataGridRowEventArgs e)
        {
            base.OnLoadingRow(e);
        }

        protected override void OnUnloadingRow(DataGridRowEventArgs e)
        {
            base.OnUnloadingRow(e);
        }
    }
}

【讨论】:

  • 尝试了一个完整的模型,但效果很好。请参阅更新的答案。过早删除不相关的 cmets 以清理帖子。
猜你喜欢
  • 2011-06-04
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 2015-05-16
  • 2019-06-11
  • 2014-05-22
  • 2015-10-10
  • 1970-01-01
相关资源
最近更新 更多