Поскольку вы просто хотите изменить цвет переднего плана другой кнопки при нажатии одной из двух кнопок, вам не нужно получать экземпляр другой кнопки. Использование Behaviors SDK может быть лучшим выбором в этом сценарии.
Ниже приводится код XAML Я проверил:
<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>
Ниже выхода:
Пожалуйста, укажите коды вы пробовали до сих пор. –
Загружено событие для каждой кнопки и сохранит его в закрытом поле. – adminSoftDK