簡體   English   中英

無法將事件發送到C#/ WPF中的另一個類

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

我對OOD,C#,WPF相對較新,但嘗試學習。 因此,我有一個主要的XAML,其中包含一些控件。 一種控件是DataGrid(dtGrid)。 DataGrid背后有自己的代碼,並且類具有一些方法。 我正在嘗試為水平滾動時創建一個事件。 我有這個:

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

我看不到dtGrid如何顯示主要XAML。 由於dtGrid(DataGrid控件)在其后面放置了此方法,因此具有自己的代碼,因此,我沒有按名稱引用主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>

我在此XAML后面有代碼,當滾動更改時,我需要在其中更新另一個對象。 但是,我的問題是我有另一個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" >

然后,我也將代碼隱藏在此XAML的后面。 因此,我不確定一切進展順利,誰可以與誰溝通。

數據網格的事件是公共的,應在數據網格之外使用,最有可能在托管它的Window或UserControl中使用:

在窗口中(或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
    }
}

更新資料

如果要查看如何從XAML訂閱事件,可以使用:

<DataGrid ScrollChanged="DoSomething" />

暫無
暫無

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

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