簡體   English   中英

如何在Mainwindow舉辦活動

[英]How to raise event at Mainwindow

我有usercontrol,它有datagrid。這個用戶控件被添加到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事件?

您無法綁定事件,例如您必須在主窗口上執行此類操作:

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

這是我在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM