简体   繁体   English

WPF从模板获取Datagrid上的选定行

[英]WPF Get Selected Row on Datagrid From Template

App.xaml App.xaml中

<Application.Resources>
    <Style x:Key="datagridRowStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="Height" Value="100"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border BorderBrush="Black" BorderThickness="1" MouseDown="row_MouseDown" Background="White">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

App.xaml.cs App.xaml.cs

public partial class App : Application
    {
        private void row_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            /* I WANT TO KNOW WHICH ROW ON THE DATAGRID I CLICKED */
            Navegacao.Switch(new service(/* SO I CAN USE IT HERE */));
        }
    }

index.xaml index.xaml

<Border BorderBrush="Black" BorderThickness="1">
    <DataGrid Name="datagrid1" Margin="50,50,50,50" ItemsSource="{Binding Path=MyDataBinding}" RowStyle="{StaticResource datagridRowStyle}" HeadersVisibility="None" VerticalScrollBarVisibility="Auto" CanUserAddRows="False"/>
</Border>

index.xaml.cs index.xaml.cs

public partial class index : Page
{
    public index()
    {
        InitializeComponent();
        BindGrid();
    }

    private void BindGrid()
    {
        DataSet bind = database.BindGrid("SELECT * FROM (projecto.encomenda INNER JOIN projecto.encom_contem ON id_enc = encom_id) INNER JOIN (SELECT codigo, tipo, descricao FROM Projecto.Produto INNER JOIN Projecto.Servico ON codigo = produto_codigo) AS T1 ON produto_codigo = codigo WHERE estado <> 'Pronto'");
        datagrid1.DataContext = bind;
    }
}

As you can see I created a template containing a border for each of the rows on the datagrid. 如您所见,我创建了一个模板,该模板包含数据网格上每一行的边框。 The question is, how can I know which row on the datagrid I clicked. 问题是,我怎么知道我单击了数据网格的哪一行。 Is there a way to know if I clicked the first border, or the second, or the others? 有没有办法知道我单击的是第一个边框还是第二个边框?

DataGridRow will be the visual parent of the sender which will be Border . DataGridRow将是发件人的可视父级,该发件人将是Border You can get that using VisualTreeHelper . 您可以使用VisualTreeHelper获得它。

private void row_MouseDown(object sender, MouseButtonEventArgs e)
{
    DataGridRow row = (DataGridRow)System.Windows.Media.VisualTreeHelper
                                   .GetParent((Border)sender);
}

On a side note you can travel up the parent visual tree recursively using this helper method. 附带说明一下,您可以使用此帮助器方法递归地移动到父视觉树。 In case interested in row index on which mouse is down, it can be calculated easily as well. 如果对鼠标按下的行索引感兴趣,也可以很容易地计算出来。

private void row_MouseDown(object sender, MouseButtonEventArgs e)
{
    DataGridRow row = FindParent<DataGridRow>((Border)sender);
    DataGrid dataGrid = FindParent<DataGrid>(row);

    int rowIndex = dataGrid.ItemContainerGenerator.IndexFromContainer(row);
}

private T FindParent<T>(DependencyObject child) where T : DependencyObject
{
    var parent = System.Windows.Media.VisualTreeHelper.GetParent(child);

    if (parent is T || parent == null)
        return parent as T;
    else
        return FindParent<T>(parent);
}

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

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