C# 如何在主窗口引发事件

C# 如何在主窗口引发事件,c#,wpf,user-controls,attachedbehaviors,C#,Wpf,User Controls,Attachedbehaviors,我有一个usercontrol,它有datagrid。这个usercontrol被添加到WPF主窗口。我正在通过bubble事件处理gridrow selection changed事件 <ListBox x:Name="myListBox" Grid.Row="0" ItemsSource="{Binding Path=_myControl}" ScrollViewer.VerticalScrollBarVisibility=

我有一个usercontrol,它有datagrid。这个usercontrol被添加到WPF主窗口。我正在通过bubble事件处理gridrow selection changed事件

    <ListBox x:Name="myListBox" Grid.Row="0"
             ItemsSource="{Binding Path=_myControl}" 
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             SelectedItem="{Binding CurrentItem}" SelectedIndex="1">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <local:UCSearchEntity GridRowSelectionConfirmed="{Binding Path=UCSearchEntity_GridRowSelectionConfirmed}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>


      public class MyViewModel:INotifyPropertyChanged
     {

     }

公共类MyViewModel:INotifyPropertyChanged
{
}
错误是“System.Windows.Data.Binding”上的
提供值引发了异常。


如何在主窗口viewModel中访问此usercontrol事件

您不能绑定到这样的事件,您必须在主窗口上执行以下操作:

<Window DataGrid.GridRowSelectionConfirmed="GridRowSelectionConfirmed">

GridRowSelectionConfirmation将是主窗口中的一个方法 上面的xaml是主窗口的xaml中的一个片段

如果您想坚持使用MVVM,那么您必须开始使用行为,但这是一个更高级的概念。该行为需要附加一个命令,您可以将该命令数据绑定到一个事件,否则该事件将无法像您试图做的那样绑定。你看,我正在利用交互性,如果你想做同样的事情,你需要blend sdk。例如:

public class AddingNewItemBehavior : Behavior<DataGrid>
{
    public static readonly DependencyProperty CommandProperty
        = DependencyProperty.Register("Command", typeof(ICommand), typeof(AddingNewItemBehavior), new PropertyMetadata());

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    protected override void OnAttached()
    {
        AssociatedObject.AddingNewItem += AssociatedObject_OnAddingNewItem;
    }

    private void AssociatedObject_OnAddingNewItem(object sender, AddingNewItemEventArgs addingNewItemEventArgs)
    {
        AddingNewItem addingNewItem = new AddingNewItem();
        Command.Execute(addingNewItem);
        addingNewItemEventArgs.NewItem = addingNewItem.NewItem;
    }
}
公共类AddingNewItemBehavior:Behavior
{
公共静态只读DependencyProperty CommandProperty
=DependencyProperty.Register(“命令”、typeof(ICommand)、typeof(AddingNewItemBehavior)、new PropertyMetadata());
公共ICommand命令
{
获取{return(ICommand)GetValue(CommandProperty);}
set{SetValue(CommandProperty,value);}
}
受保护的覆盖无效附加()
{
AssociatedObject.AddingNewItem+=AssociatedObject\u OnAddingNewItem;
}
私有void Associated object_on AddingNewItem(对象发送方,AddingNewItemEventArgs AddingNewItemEventArgs)
{
AddingNewItem AddingNewItem=新建AddingNewItem();
Command.Execute(addingNewItem);
addingNewItemEventArgs.NewItem=addingNewItem.NewItem;
}
}
这是我在datagrid上添加的新行为

这是一个简单的例子,我利用了这种行为:

<UserControl x:Class="Interstone.Configuratie.Views.GraveerFiguurAdminUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:iCeTechControlLibrary="clr-namespace:ICeTechControlLibrary;assembly=ICeTechControlLibrary"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <DataGrid ItemsSource="{Binding ZandstraalImageTypes.View}" AutoGenerateColumns="False"
              VerticalGridLinesBrush="#FFC9CACA" HorizontalGridLinesBrush="#FFC9CACA" RowHeaderWidth="50" 
              >
        <i:Interaction.Behaviors>
            <iCeTechControlLibrary:AddingNewItemBehavior Command="{Binding AddingNewCommand}"/>
        </i:Interaction.Behaviors>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Categorie" Binding="{Binding TypeNaam}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>


您不能对这样的事件进行绑定。@PhilipStuyck不违反MVVM吗?啊,但这是一个完全不同的问题,是的,确实如此,但您需要使用行为等高级概念。“行为”的名称空间是什么?System.Windows.Interactivity;混合sdk的一部分