Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding context in ContentPresenter #23797

Open
smalec opened this issue Jul 24, 2024 Discussed in #22161 · 3 comments
Open

Binding context in ContentPresenter #23797

smalec opened this issue Jul 24, 2024 Discussed in #22161 · 3 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors t/bug Something isn't working
Milestone

Comments

@smalec
Copy link

smalec commented Jul 24, 2024

Reporting this as bug, since it looks fairly similar to #12470

Discussed in #22161

Originally posted by smalec May 2, 2024
Hello, I'm still new in .NET MAUI so I still have some problems with basic concepts and best practices. Currently I'm struggling a bit with ContentPresenter.

So I have a custom component (ContentView) with ControlTemplate that uses ContentPresenter. Let us name this CustomControl.xaml

<ContentView>
    <ContentView.ControlTemplate>
        <ControlTemplate>
            <ContentPresenter />
        </ControlTemplate>
    </ContentView.ControlTemplate>
</ContentView>

Obviously I'm using MVVM so when I want to use CustomControl in a view I do this:

<ContentPage>
    <local:CustomControl>
        <Button Text="Click me" Command="{Binding DoSomethingCommand}" />
    </local:CustomControl>
</ContentPage>

where DoSomethingCommand is defined in my view model.

And this works perfectly fine, awesome! But what if I want to use multiple ContentPresenters in my CustomControl? As far as I know, I should bind content of ContentPresenter like this:

<ContentView>
    <ContentView.ControlTemplate>
        <ControlTemplate>
            <ContentPresenter Content="{TemplateBinding MyContent}" />
        </ControlTemplate>
    </ContentView.ControlTemplate>
</ContentView>

Of course I've defined BindableProperty in my code-behind file. And now I'm using my CustomControl like this:

<ContentPage>
    <local:CustomControl>
        <local:CustomControl.MyContent>
            <Button Text="Click me" Command="{Binding DoSomethingCommand}" />
        </local:CustomControl.MyContent>
    </local:CustomControl>
</ContentPage>

And my button shows up exactly where it should, but... there is absolutely no effect when clicking on it :( I searched for a solution and I found out that I can use it like that:

<Button Text="Click me" Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:MyViewModel}}, Path=DoSomethingCommand}" />

And that indeed works, but it looks awful and I don't understand it at all :( Could anyone please help me understand why this simple binding to my command doesn't work? Maybe I can/should modify somehow my CustomControl?

Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@mattleibow mattleibow added this to the Backlog milestone Jul 24, 2024
@mattleibow mattleibow added area-xaml XAML, CSS, Triggers, Behaviors t/bug Something isn't working labels Jul 24, 2024
@mattleibow
Copy link
Member

@StephaneDelcroix thoughts?

@agrisyst-slo
Copy link

I have the same problem.
I thought that fix #12536 would solve it, but I have just updated VS to version 17.11.2 and it still does not work.
See the attached test app.
TestMauiAppBindings.zip

If I made a mistake in the controls, please let me know.

Kind regards, Saskia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants