-
Notifications
You must be signed in to change notification settings - Fork 112
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
EventTriggerBehavior does not fire bound command in DataTemplate #239
Comments
@sam-wheat This issue should be closed as the command does get invoked in the latest version of your repo proj. |
@kmgallahan Thank you very much for your replies. The code written above is written according to the documentation found here. At the time I wrote this ticket, my repo reflected this exact code and it does not work. Did I write it incorrectly? Perhaps I am missing something, or perhaps the documentation needs to be updated. In any case, I think this is a fair question as stated and should be closed with a definitive response. It will certainly help me - and hopefully other developers who come across this issue. The code in my repo does indeed work. After writing this ticked I continued working on the problem and literally by guesswork alone I stumbled across the solution you now see in my repo. As I requested here the question is now "why does it work?". Did I discover a way of doing it wrong that just happens to work? At the very minimum we have a documentation issue - I can find nothing from MS that suggests the approach I've taken is valid or even supported. In fact I think a good case can be made that my approach is completely wrong, and that |
I did not notice that the code in the repo was different - above you use I'm not entirely sure why RelativeSource Self doesn't work. It is unlikely to be a problem with code in this repo (more likely a XAML compilation issue).
I replied there before my above comment. As mentioned there, you are running into some preexisting Binding issues. Again, I'm not sure what exact syntax you are referring to when mentioning Just as a note, you appear to be creating a Control, whereas I would be creating a UserControl instead. A Control uses a .cs file and Style in a ResourceDictionary, while a UserControl uses a .xaml file with .cs file code behind. The former is generally for creating reusable controls that could go into a UI library, for example. The latter is for building view parts/components for an application (where you would be directly making references to business logic in command implementations). |
Very sorry for the confusion. I posted this issue and then kept working on it. I found a way to make it work - but I don't understand why - hence this post. For clarity, the working code is below with comments on relevant lines.
Also - I am creating a reusable (templated) control. Others have suggested I use x:Bind and a use a resouce dictionary with code behind but I know of no (working) way to do that. |
This is using
Where you attached the Ancestor property to If you omitted ElementName, then you'd have to specify a RelevantSource or Source. If you didn't use either then on creating the Binding the compiler wouldn't know what object to use the Path on:
Similarly, if you omitted the Path part:
If you are asking why
|
True - in fact As far as the working code, I imagine this XAML:
becomes something like this at run time because
Why would IDK why If anything, using |
To do only what and referencing what documentation?
Binding defaults its source object the to current DataContext. The DataContext in your code at the top is automatically set to the data object being templated inside your DataTemplate. It is why you can just use If you weren't inside a DataTemplate then the ElementName could be omitted (depending on where Ancestor was attached). |
Agree it seems wrong but as I mentioned in my last comment is the documented syntax.
I don't see how that makes a difference. Either should work on their own IMO. Using them together in any circumstance is at best redundant and possibly contradictory. |
The problems you are running into involve data binding, which isn't the focus of the FrameworkElement Extensions API docs. I would review: As per the title of the linked page: Property value inheritance (WPF .NET) That is for WPF, and you are not working with WPF. This is WASDK/WinUI 3, which uses essentially the same XAML functionality as UWP. There is no such thing here: https://stackoverflow.com/questions/43424845/dependencyproperty-value-inheritance-equivalent-in-uwp
Again, you are inside a DataTemplate, so the default binding source object (that which is used when omitting ElementName/RelativeSource/Source) for Binding is the current data object. If you manually set |
Got it, thank you. |
In the XAML below the
Toggled
event does not fire the boundPlaceSelectedCommand
.Repo is here. Possibly related to this. Could also be related to this.
The text was updated successfully, but these errors were encountered: