0

I'm trying to have a path in a datagrid. I wrote the following data template:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding AlertProgram}" IsEnabled="False" Grid.Column="0" />
                <Button Content="..." Grid.Column="1" Click="Button_browse_alert_program" />
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

(I wrote it straight into the <DataGrid.columns> elelment)

In the function Button_browse_alert_program I open a OpenFileDialog to get a file.

My problem is - I can't access the text box. I tried giving it a name using x:Name="someName" but the compiler complains that the name doesn't exist.

I have seen the following SO questions: How to access datagrid template column textbox text WPF C#, How to access control in DataGridTemplateColumn to get value?

Is there a way to do this without third party software, and without writing my own FindChild function?

(I'm quite new to WPF, so if you have other comments on my code - speak up)

Community
  • 1
  • 1
elyashiv
  • 3,623
  • 2
  • 29
  • 52

2 Answers2

0

Technically you should have some implementation of ICommand, bind Button.Command to your ViewModel and do what you want in there but if you're fixed on doing it this way then you can access DataConext through sender in Button_browse_alert_program

private void Button_browse_alert_program(object sender, RoutedEventArgs e)
{
   var value = ((sender as Button).DataContext as MyItemClass).AlertProgram;
}
dkozl
  • 32,814
  • 8
  • 87
  • 89
0

Why would you do all that? Why thrid party software?

Take a look at this:

    <DataGrid x:Name="dG">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox x:Name="tbx">test</TextBox>
                            <Button Content="click me" Grid.Column="1" Click="OnClick" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.ItemsSource>
            test
        </DataGrid.ItemsSource>
    </DataGrid>

This is code behind:

    private void OnClick(object sender, RoutedEventArgs e)
    {
        TextBox tbx = null;
        // grid is a panel, panels have children
        var parent = VisualTreeHelper.GetParent((DependencyObject)sender) as Panel;
        foreach(var child in parent.Children)
        {
            if (child is TextBox)
            {
                tbx = (TextBox)child;
                break;
            }
        }

        // you should check if tbx != null
        tbx.Text = "Hello";
    }
dev hedgehog
  • 8,698
  • 3
  • 28
  • 55