简体   繁体   English

无法将事件发送到C#/ WPF中的另一个类

[英]Trouble sending an event to another class in C#/WPF

I'm relatively new to OOD, C#, WPF, but trying to learn. 我对OOD,C#,WPF相对较新,但尝试学习。 So I have a main XAML that has a few controls in it. 因此,我有一个主要的XAML,其中包含一些控件。 One control is a DataGrid (dtGrid). 一种控件是DataGrid(dtGrid)。 The DataGrid has its own code behind and has some methods for its class. DataGrid背后有自己的代码,并且类具有一些方法。 I am trying to create an event for when the scroll is done horizontally. 我正在尝试为水平滚动时创建一个事件。 I have this: 我有这个:

 private void dtGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (e.HorizontalChange != 0)
        {
            // update some stuff to main XAML
        }
    }

I don't see how the dtGrid has any visibility of the main XAML. 我看不到dtGrid如何显示主要XAML。 Since the dtGrid (DataGrid control) has its own code behind it where I put this method, I have no reference to the other controls that are in the main XAML by their Name. 由于dtGrid(DataGrid控件)在其后面放置了此方法,因此具有自己的代码,因此,我没有按名称引用主XAML中的其他控件。 Is there a way to get around this? 有办法解决这个问题吗? Thanks. 谢谢。

Edit: Additional Code and some psuedo code for brevity So my main class that has the main XAML: 编辑:附加代码和简短的一些伪代码,所以我的主要类具有主要XAML:

<UserControl>
<GroupBox Header="Sample" Grid.Row="2" Margin="5, 0, 5, 0" FontSize="12" FontFamily="Arial" FontWeight="Bold">
            <View:SampleControl x:Name="SampleControl" Background="Transparent" />
        </GroupBox>
</UserControl>

I have code behind this XAML where I need to make the update to the other object when the scroll is changed. 我在此XAML后面有代码,当滚动更改时,我需要在其中更新另一个对象。 However, the problem I have is I have another XAML for the SampleControl which is: 但是,我的问题是我有另一个XAML用于SampleControl:

<some UserControl and the namespaces>
<DataGrid x:Name="dtGridReads"  AutoGenerateColumns="False" 
            VirtualizingStackPanel.IsVirtualizing="True"                                       
            VirtualizingStackPanel.VirtualizationMode ="Standard" 
              EnableColumnVirtualization="False"
              EnableRowVirtualization="False"
            ScrollViewer.IsDeferredScrollingEnabled="True"
            CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="True"
             ItemsSource ="{Binding}" Block.TextAlignment="Center"
             CanUserAddRows="False" CanUserDeleteRows="False" FrozenColumnCount="1"
               GridLinesVisibility="None" FontFamily="Arial" FontSize="10" Background="White"
              ScrollViewer.ScrollChanged="dtGridReads_ScrollChanged" >

and then I have code-behind behind this XAML as well. 然后,我也将代码隐藏在此XAML的后面。 So I'm not sure where everything goes and who can communicate to who. 因此,我不确定一切进展顺利,谁可以与谁沟通。

The event of the datagrid is public and should be used outside the datagrid, most likely in the Window or UserControl that's hosting it: 数据网格的事件是公共的,应在数据网格之外使用,最有可能在托管它的Window或UserControl中使用:

In window (or any class other than the DataGrid class) 在窗口中(或DataGrid类以外的任何其他类)

DataGrid dg = new DataGrid();
dg.ScrollChanged += DoSomething;


private void DoSomething(object sender, ScrollChangedEventArgs e)
{
    if (e.HorizontalChange != 0)
    {
        // update some stuff to main XAML which should now be available
    }
}

Update 更新资料

If you want to see how to subscribe to the event from XAML you can use: 如果要查看如何从XAML订阅事件,可以使用:

<DataGrid ScrollChanged="DoSomething" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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