1

I want to access the element UserCodeTextBlock in my code but for some reason I can't.

The idea is to show the TextBlock and Button, and after clicking the Button they both go invisible and only the TextBox is shown so i can change it's value.

This is all contained in a DataGridTemplateColumn as shown in this code:

XAML

 <DataGridTemplateColumn x:Name="user_codeColumn" Header="{x:Static p:Resources.User_user_code}" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate x:Name="UserCodeTemplate">
                                        <StackPanel x:Name ="UserCodeStack" Orientation="Horizontal" HorizontalAlignment="Right">
                                            <TextBlock x:Name="UserCodeTextBlock" Text="{Binding UserCode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }" Visibility="Visible"/>
                                            <TextBox x:Name="UserCodeTextBox" Text="{Binding UserCode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }" Visibility="Collapsed"/>
                                            <Button Content="A" Click="UserCodeButton_Click"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

In my code I have an Event that executes when the Button is clicked. I'm trying to read XAML elements using a function that I found somewhere but it doesn't seem to work.

C#

public static T GetChildOfType<T>(this DependencyObject depObj)
    where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }

    private void UserCodeButton_Click(object sender, RoutedEventArgs e)
    {
        InitializeComponent();

        var UserCodeTextBlock_ = this.user_codeColumn.GetChildOfType<DataTemplate>();

        Button UserCodeButton_ = e.Source as Button;

        UserCodeButton_.Visibility = Visibility.Hidden;

        MessageBox.Show(Properties.Resources.CodeChanged, "", MessageBoxButton.OK);

    }

I've tried a lot of things already but I couldn't make it work as I wish. Btw, I'm a newb in C# and WPF. Thanks in advance for any help!

Jack
  • 886
  • 7
  • 27
Ginjo
  • 31
  • 6
  • 3
    Why not create a property and bind it to visibility for your textblock? When you click on your button, IsVisible= false. Your binding need to have elementName attibute to change the datacontext. – MatDev8 Jan 10 '18 at 15:15
  • You could also create a `Style` for those elements with a ToggleButton and Bind the the `IsChecked` proeprty. BTW `TwoWay` Binding on a `TextBlock` doesn't make sense as it will never be changed by the `TextBlock`. – XAMlMAX Jan 10 '18 at 15:42
  • why not `IsReadOnly="False"` in a `DataGridTextColumn` so the column can be edited if you click on it – Celso Lívero Jan 10 '18 at 16:44

0 Answers0