【问题标题】:How to raise event at Mainwindow如何在主窗口引发事件
【发布时间】:2015-04-27 06:54:00
【问题描述】:

我有具有数据网格的用户控件。此用户控件已添加到 WPF 主窗口。我正在通过气泡事件处理网格选择更改事件。

    <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 中访问这个用户控件事件?

【问题讨论】:

  • 你不能对这样的事件进行绑定。
  • @PhilipStuyck 不违反 MVVM 吗?
  • 啊,但这是一个完全不同的问题,是的,确实如此,但是您需要使用高级概念,例如行为。

标签: c# wpf user-controls attachedbehaviors


【解决方案1】:

你不能绑定到这样的事件,你必须在你的主窗口上做这样的事情:

<Window DataGrid.GridRowSelectionConfirmed="GridRowSelectionConfirmed">

并且 GridRowSelectionConfirmed 将是您主窗口中的一个方法 上面的 xaml 是主窗口 xaml 中的一个 sn-p。

如果你想坚持使用 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>

【讨论】:

  • “行为”的命名空间是什么?
  • System.Windows.Interactivity; blend sdk的一部分
猜你喜欢
  • 1970-01-01
  • 2015-03-21
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2013-01-06
相关资源
最近更新 更多