gpt4 book ai didi

silverlight - CustomDataGrid的命令中的命令参数为null

转载 作者:行者123 更新时间:2023-12-03 10:50:55 25 4
gpt4 key购买 nike

这段代码可以正常工作:

<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为null:
<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驱动,并向其中添加了一个事件。 override有两个 event: OnLoadingRowOnUnLoadingRow

请注意,是一个简单的 CommandParameter,例如一个简单的字符串发送很好。

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

问题是什么?

最佳答案

好的,我已经为您的情况构建了完整的模型并在下面提供了所有代码。一切都是白手起家的,所以每个人都请减少我输入的不良做法的数量:)

糟糕的是,我的派生类的工作原理与普通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();
}
}
}

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);
}
}
}

关于silverlight - CustomDataGrid的命令中的命令参数为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com