如何在Mainwindow举办活动

时间:2015-02-25 10:15:08

标签: c# wpf user-controls attachedbehaviors

我有usercontrol,它有datagrid。这个usercontrol被添加到WPF主窗口。我正在通过bubble事件处理gridrow选择更改事件。

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

     }

错误为Provide value on 'System.Windows.Data.Binding' threw an exception.

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

1 个答案:

答案 0 :(得分:0)

您不能在主窗口上绑定诸如此类事件之类的事件:

<Window DataGrid.GridRowSelectionConfirmed="GridRowSelectionConfirmed">

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

如果你想坚持使用MVVM,那么你必须开始使用行为,但这是一个更高级的概念。需要这样的行为来附加一个命令,您可以将该数据绑定到一个事件,否则该事件不像您尝试那样可绑定。你看我正在利用交互性,如果你想做同样的事情,你需要混合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;
    }
}

这是我在数据网格上添加的新行为。

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

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