thankyoukindly
thankyoukindly

Reputation: 35

Universal Windows App: Two buttons inside of ListViewItem

I have two buttons inside the DataTemplate of my Listview.ItemTemplate.

I have no problem accessing each one on their own using a Click event, however, when I click one of the buttons I want to change the foreground color of the other button. How do I get the instance of the other button (that wasn't clicked) in the ListViewItem?

Upvotes: 0

Views: 535

Answers (1)

Jerry Li
Jerry Li

Reputation: 1042

Since you just want to change the foreground color of the other button when you click one of the two buttons, you don't have to get the instance of the other button. Using Behaviors SDK may be a better choice in this scenario.

Following is the xaml code I have verified:

<Page
x:Class="ListViewWithTwoButtonDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewWithTwoButtonDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Interactions="using:Microsoft.Xaml.Interactions.Core"
mc:Ignorable="d">

<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="400">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
                <TextBlock Grid.Column="1" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
                <!--Use Behaviors SDK here, and no code behind is needed -->
                <Button Grid.Column="2" x:Name="btn1" Content="Button 1">
                    <Interactivity:Interaction.Behaviors>
                        <Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=btn1}">
                            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=btn2}" PropertyName="Foreground" Value="Green" />
                        </Interactions:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </Button>
                <!--Use Behaviors SDK here, and no code behind is needed -->
                <Button Grid.Column="3" x:Name="btn2" Content="Button 2">
                    <Interactivity:Interaction.Behaviors>
                        <Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=btn2}">
                            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=btn1}" PropertyName="Foreground" Value="Green" />
                        </Interactions:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </Button>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
</Page>

Following is the output: enter image description here

Upvotes: 1

Related Questions