0

I have the following ContentView:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ValidationLibrary.ValidationFor"
             x:Name="this">
    <ContentView.Content>
        <Grid RowDefinitions="Auto, *">
            <ContentPresenter x:Name="chiledContainer" Grid.Row="0" />
            <Label Text="{Binding ErrorMessage, Source={x:Reference this}}" 
                   IsVisible="{Binding IsValid, Source={x:Reference this}}"/>
        </Grid>
    </ContentView.Content>
</ContentView>

In the code behind I need to access the first child in the ContentPresenter.

Exemple usege is below:

<validation:ValidationFor FieldName="Name">
    <Entry x:Name="Name" Text="{Binding Name, Mode=TwoWay, UpdateSourceEventName=PropertyChanged}" />
</validation:ValidationFor>

Anything I tried I did not work. I could access the grid, contentpresenter, label but not the child or any element of the content presenter.

thnx

2
  • What exactly are you trying to accomplish? Directly accessing view elements like this is generally not a good idea Commented Oct 22, 2023 at 18:01
  • I am trying to make a validation library, and I just wan to access the value in the Entry. Commented Oct 22, 2023 at 20:55

1 Answer 1

0

Bad idea.

Do not try to name your controls and use their names to access them during validation. You will eventually get it working somehow, but then you will have to implement the same logic for templates. What if you have to validate controls in CollectionView? You will give them the same name, and try to get their value?

You should not be validating controls at all.

You need to validate properties. The control (no matter what kind) is modifying the properties. When you need to do validation, you go over all your properties that require validation, and compare with the acceptable values.

At your place, I would open ObservableValidator source of CommunityToolkit to "draw inspiration" (notice how I didn't say steal ideas).

Edit: I have read your comment, and I understand that you really want your control naming idea to happen, but it will not work.

Think for a situation, where you have a list of entries, and you try to specify name of your control in the template.

You can't have 20 entry controls, all named "myEntry". This is more than obvious.

If you do not like the concept I proposed about property validation, and you want to "wrap" controls. Maybe you can implement Behavior.

This is another way of validating, where you set behavior directly to the controls in the XAML. And then usually the feedback to the user is happening via styling.

(In either case, you will not be naming your controls, this is non-sense)

Again - very good place to start will be TextValidationBehavior, from the CommunityToolkit. It will not harm you to check it out.

Sign up to request clarification or add additional context in comments.

2 Comments

I disagree. If I wrap every input element then there will be no name founding issue. Still my problem is how to access the wrapped child element.
@Wasyster If you really want to access child elements, you should search VisualTree, and learn about it. I warn you again - this approach is the wrong way to go. I give you another alternative for implementing validation, that is more control-focused than the last I gave you. Direct access to the controls will lead to nothing good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.