Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在按钮单击事件期间,无法获取DataGridRow的索引。有时-1。_C#_Datagrid_Indexing_Sender - Fatal编程技术网

C# Can';在按钮单击事件期间,无法获取DataGridRow的索引。有时-1。

C# Can';在按钮单击事件期间,无法获取DataGridRow的索引。有时-1。,c#,datagrid,indexing,sender,C#,Datagrid,Indexing,Sender,我遇到了Dispatchermer占用太多资源的问题。我希望有人能帮忙 我正在使用XAML在屏幕上显示项目列表。列表的每一行都有一个按钮,用于修改该行的信息。代码隐藏需要确定单击了列表的哪一行。这是代码的神奇部分,正在分解: int row_index = DataGridRow.GetRowContainingElement(sender as FrameworkElement).GetIndex(); 有时时间行索引设置正确,有时返回-1为什么它不总是返回正确的索引?更让我沮丧的是,我可以

我遇到了
Dispatchermer
占用太多资源的问题。我希望有人能帮忙

我正在使用XAML在屏幕上显示项目列表。列表的每一行都有一个按钮,用于修改该行的信息。代码隐藏需要确定单击了列表的哪一行。这是代码的神奇部分,正在分解:

int row_index = DataGridRow.GetRowContainingElement(sender as FrameworkElement).GetIndex();
有时时间行索引设置正确,有时返回-1为什么它不总是返回正确的索引?更让我沮丧的是,我可以使用调试器在DataGridRow结构中看到正确的索引。正确的索引在那里,但是
GetIndex()
有时仍然返回-1

接口代码:

<Window x:Class="WhyNoButtonClick.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WhyNoButtonClick" Height="265" Width="306">
    <Grid Margin="0,0,0,1">
        <StackPanel Height="192" VerticalAlignment="Top">
            <DataGrid x:Name="dg_driver_list" AutoGenerateColumns="False" Height="186">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Name">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label x:Name="col_driver_name" Content="{Binding Name}" TextBlock.LineStackingStrategy="BlockLineHeight" TextBlock.LineHeight="5" FontSize="12"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Time remaining">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label x:Name="col_driver_time_remain" Content="{Binding TimeRemain}" TextBlock.LineStackingStrategy="BlockLineHeight" TextBlock.LineHeight="5" FontSize="12"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Action button">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="btn_driver_hit" PreviewMouseDown="btn_driver_hit_Click" Content="Hit" Height="20" TextBlock.LineStackingStrategy="BlockLineHeight" TextBlock.LineHeight="5" FontSize="12"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
        <StackPanel Margin="105,192,105,10">
            <Button x:Name="btn_pause_active" Content="Start/Pause" ToolTip="Start/Pause stopwatches for all active drivers" Height="22" Click="btn_start_pause_active_Click"/>
        </StackPanel>
    </Grid>
</Window>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Media;

namespace WhyNoButtonClick
{
    public partial class MainWindow : Window
    {
        // ### Modify this to increase or decrease the chances of getting the correct index.
        TimeSpan screen_refresh_delay = new TimeSpan(0, 0, 0, 0, 025);

        DispatcherTimer update_timer_dispatch = new DispatcherTimer(DispatcherPriority.Normal);
        List<Stopwatch> driver_hit_timer_watches = new List<Stopwatch>();
        List<Participant> driver_list = new List<Participant>();
        SolidColorBrush color_red = new SolidColorBrush(Color.FromRgb(255, 0, 0));  // Red
        SolidColorBrush color_black = new SolidColorBrush(Color.FromRgb(0, 0, 0));  // Black

        TimeSpan hit_timer = new TimeSpan(0, 0, 60);
        TimeSpan time_almost_up = new TimeSpan(0, 0, 0, 59);
        TimeSpan time_remaining = new TimeSpan(0, 0, 0);

        public MainWindow()
        {
            InitializeComponent();

            Label driver_name_01 = new Label();
            Label driver_time_remain_01 = new Label();
            driver_name_01.Content = "adam (1)";

            Label driver_name_02 = new Label();
            Label driver_time_remain_02 = new Label();
            driver_name_02.Content = "ben (2)";

            Label driver_name_03 = new Label();
            Label driver_time_remain_03 = new Label();
            driver_name_03.Content = "chad (3)";

            driver_list.Add(new Participant(0, driver_name_01, driver_time_remain_01));
            driver_list.Add(new Participant(1, driver_name_02, driver_time_remain_02));
            driver_list.Add(new Participant(2, driver_name_03, driver_time_remain_03));

            for (Int16 driver_index = 0; driver_index < driver_list.Count; driver_index++)
            {
                driver_hit_timer_watches.Add(new Stopwatch());
                driver_hit_timer_watches[driver_index].Stop();
                driver_hit_timer_watches[driver_index].Reset();
            }

            dg_driver_list.ItemsSource = driver_list;

            update_timer_dispatch.Interval = screen_refresh_delay;
            update_timer_dispatch.Tick += new EventHandler(update_timer_dispatch_tick);
            update_timer_dispatch.Start();
        }

        void update_timer_dispatch_tick(object sender, EventArgs e)
        {
            for (Int16 driver_index = 0; driver_index < driver_hit_timer_watches.Count; driver_index++)
            {
                time_remaining = hit_timer - driver_hit_timer_watches[driver_index].Elapsed;

                if (time_remaining.CompareTo(time_almost_up) < 0)
                {
                    driver_list[driver_index].Name.Foreground = color_black;
                    driver_list[driver_index].TimeRemain.Foreground = color_black;
                }

                driver_list[driver_index].TimeRemain.Content = String.Format("{0:0}:{1:00}.{2:0}", time_remaining.Minutes.ToString(), time_remaining.Seconds.ToString(), (time_remaining.Milliseconds / 100).ToString());
            }

            dg_driver_list.Items.Refresh();
        }

        private void btn_driver_hit_Click(object sender, RoutedEventArgs e)
        {
            int row_index = DataGridRow.GetRowContainingElement(sender as FrameworkElement).GetIndex();
            if (row_index > -1)
            {
                driver_list[row_index].Name.Foreground = color_red;
                driver_list[row_index].TimeRemain.Foreground = color_red;
                if (driver_hit_timer_watches[row_index].IsRunning)
                {
                    driver_hit_timer_watches[row_index].Restart();
                }
                else
                {
                    driver_hit_timer_watches[row_index].Reset();
                }
            }
            else
            {
                // I need to know what row to modify but don't have an index!
                // ### Debug - Breakpoint here.
                DataGridRow myrow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
                int myindex = myrow.GetIndex();
            }
        }

        private void btn_start_pause_active_Click(object sender, RoutedEventArgs e)
        {
            for (Int16 driver_index = 0; driver_index < driver_hit_timer_watches.Count; driver_index++)
            {
                if (driver_hit_timer_watches[driver_index].IsRunning)
                {
                    driver_hit_timer_watches[driver_index].Stop();
                }
                else
                {
                    driver_hit_timer_watches[driver_index].Start();
                }
            }
        }
    }

    public class Participant
    {
        public Int16 Index { get; set; }
        public Label Name { get; set; }
        public Label TimeRemain { get; set; }

        public Participant(Int16 index, Label name, Label timeRemain)
        {
            this.Index = index;
            this.Name = name;
            this.TimeRemain = timeRemain;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用系统诊断;
使用System.Windows.Threading;
使用System.Windows.Media;
名称空间WhyNoButtonClick
{
公共部分类主窗口:窗口
{
//####修改此选项以增加或减少获得正确索引的机会。
TimeSpan屏幕\刷新\延迟=新的TimeSpan(0,0,0,0,025);
Dispatchermer update\u timer\u dispatch=新的Dispatchermer(DispatcherPriority.Normal);
列表驱动程序\点击\定时器\手表=新列表();
列表驱动程序_List=新列表();
SolidColorBrush color_red=新的SolidColorBrush(color.FromRgb(255,0,0));//红色
SolidColorBrush color_black=新的SolidColorBrush(color.FromRgb(0,0,0));//黑色
TimeSpan hit_timer=新的TimeSpan(0,0,60);
TimeSpan time\u几乎向上=新的TimeSpan(0,0,0,59);
TimeSpan time_剩余=新的TimeSpan(0,0,0);
公共主窗口()
{
初始化组件();
标签驱动程序_name_01=新标签();
标签驱动程序_时间_剩余_01=新标签();
驱动程序_name_01.Content=“adam(1)”;
标签驱动程序_name_02=新标签();
标签驱动程序_时间_剩余_02=新标签();
司机姓名02.Content=“ben(2)”;
标签驱动程序_name_03=新标签();
标签驱动程序_时间_剩余_03=新标签();
司机姓名03.Content=“乍得(3)”;
驱动程序列表。添加(新参与者(0,驱动程序名称\u 01,驱动程序时间\u保留\u 01));
驾驶员列表。添加(新参与者(1,驾驶员姓名02,驾驶员时间02));
驾驶员列表。添加(新参与者(2,驾驶员姓名03,驾驶员时间03));
对于(Int16驱动程序索引=0;驱动程序索引-1)
{
驱动程序列表[行索引].Name.Foreground=color\u red;
驱动程序列表[行索引].timeremaine.Foreground=color\u red;
if(驱动程序\命中\计时器\监视[行\索引].IsRunning)
{
驱动程序\点击\计时器\监视[行\索引]。重新启动();
}
其他的
{
驱动程序\点击\计时器\监视[行\索引].Reset();
}
}
其他的
{
//我需要知道要修改的行,但没有索引!
//#####调试-此处为断点。
DataGridRow myrow=DataGridRow.GetRowContainingElement(发送方作为FrameworkElement);
int myindex=myrow.GetIndex();
}
}
私有无效btn\u开始\u暂停\u活动\u单击(对象发送方,路由目标)
{
用于(Int16驱动程序索引=0;驱动程序索引<驱动程序命中计时器计数;驱动程序索引++)
{
如果(驱动程序命中计时器)监视[驱动程序索引].IsRunn
DataGridRow dgr_test = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
Participant one_participant = (Participant)dgr_test.Item;
int row_index = one_participant.Index;
if (row_index > -1)