简体   繁体   中英

How to raise event at Mainwindow

I have usercontrol which has datagrid .This usercontrol is added to WPF main window.I am handling gridrow selection changed event through bubble event.

    <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
     {

     }

the error is Provide value on 'System.Windows.Data.Binding' threw an exception.

How can I access this usercontrol event in my mainwindow viewModel ?

You cannot do binding to events like that you have to do something like this on your mainwindow :

<Window DataGrid.GridRowSelectionConfirmed="GridRowSelectionConfirmed">

and GridRowSelectionConfirmed would be a method in your mainwindow And the xaml above is a snippet in your xaml of the mainwindow.

If you want to stick to using MVVM then you have to start using behaviours but this is a more advanced concept. The behaviour is needed to attach a command that you can databind to an event that otherwise is not bindable like you were trying to do. You see I am making use of interactivity, if you want to do the same you need the blend sdk. An example :

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

This is an adding new behaviour I have on a datagrid.

And this is a simplified example where i make use of that behaviour :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM