I am having problems with generating a click event for items inside an items control. I am new to xaml nad wpf. Can you the experts help me out. Below is the code that i have come up with but now having no clue on how to add a click event to for the generated items. Will very much appreciate your responses. Thank you for reading
<itemscontrol itemssource="{Binding Text, Source={StaticResource TextContainer}}">
<itemscontrol.itemspanel>
<itemspaneltemplate>
<wrappanel itemwidth="100">
ItemHeight="100" />
<itemscontrol.itemtemplate>
<datatemplate datatype="{x:Type sys:String}">
<border cornerradius="100">
removed="BlueViolet">
<Button Margin="20"
Content="{Binding}"
HorizontalContentAlignment="Center" />
This is one of the basics of UI programming even developer should know. The most primitive approach is shown here:
https://msdn.microsoft.com/en-us/library/ms743596%28v=vs.110%29.aspx
[
^
].
However, rarely do it this way, only in cases where I want to share the same event handler in different controls. More often, I use
anonymous handlers
. For example,
ItemControl myParentControl =
UIElement item =
myParentControl.AddChild(item);
item.MouseDown += (sender, eventArgs) => {
UIElement uiElement = (UIElement)sender;
HandleTheEventSomehow(uiElement);
HandleTheEventInSomeOtherWay(uiElement, eventArgs.Timestamp);
As
UIElement
does not have
Click
event, I used
MouseDown
example. But you can use more derived class with
Click
(you just did not specify what it is), such as
Button
and handle this event in the same way; if you need, you can case
sender
to this UI element type.
See also:
https://msdn.microsoft.com/en-us/library/system.windows.uielement%28v=vs.110%29.aspx
[
^
],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol%28v=vs.110%29.aspx
[
^
].
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.