【问题标题】:Get Row Selected before MouseDown Event Datagrid在 MouseDown 事件 Datagrid 之前获取行选择
【发布时间】:2017-05-24 08:35:26
【问题描述】:

我在 Datagrid 中获得了我的 Image-Row 的 XAML:

<DataGridTemplateColumn  x:Name="imgSettings" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image x:Name="imgSettings"  Source="img/settings_blue.png" Stretch="None" MouseDown="Row_DoubleClick" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected, 
                  RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True">
                    <Setter TargetName="imgSettings" Property="Source" Value="/img/settings_white.png" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我的Double Click 行事件是:

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    Equipment classObj = clientDataGrid.SelectedItem as Equipment;
    string cellValue = classObj.EQNr;

    lblArtikel.Content = "Equipment: " + cellValue ;
    Dispatcher.BeginInvoke((Action)(() => tbControlETK.SelectedIndex = 1));
}

这实际上有效,但现在我尝试为这个图像按钮获取相同的功能,但这给了我一个空异常,因为 MouseDown 事件在 Row 被选中之前触发......任何解决方案?

我已经想到了一个用于图像行的函数,我可以在其中获取发送者的行或类似的东西。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以在可视化树中搜索要选择的 DataGridRow 并使用它们,如果您找到了这样一个。

    <Image x:Name="imgSettings"  Source="img/settings_blue.png" Stretch="None" MouseDown="Mouse_Down" />
    
    
    
    private void Mouse_Down(object sender, MouseButtonEventArgs e)
            {
                DataGridRow gridRowGettingSelection = null;
                var visParent = VisualTreeHelper.GetParent(sender as FrameworkElement);
                while (gridRowGettingSelection == null && visParent != null)
                {
                    gridRowGettingSelection = visParent as DataGridRow;
                    visParent = VisualTreeHelper.GetParent(visParent);
                }
                if (gridRowGettingSelection == null) { return; }
    
                Equipment classObj = gridRowGettingSelection.DataContext as Equipment;
                string cellValue = classObj.EQNr;
    
                lblArtikel.Content = "Equipment: " + cellValue;
                Dispatcher.BeginInvoke((Action)(() => tbControlETK.SelectedIndex = 1));
    
            }
    

    【讨论】:

    • 太棒了!正是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2011-07-06
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多