К сожалению, xaml не поддерживает множественные привязки.
Если кнопка в ListView
,
BindingContext="{Binding Source={x:Reference icaoDownloads}, Path=BindingContext}"
должен удалить форму Button.
Решение первое : попробуйте следующий код:
<Button
Command="{Binding BindingContext.DownloadCommand, Source={x:Reference icaoDownloads}}"
CommandParameter="{Binding Source={x:Reference Item},Path=BindingContext}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="Center"
BackgroundColor="Black"
TextColor="WhiteSmoke"
Grid.Column="1"
Text=" Update "
BorderColor="DarkSlateGray"
BorderRadius="5"
BorderWidth="2"
Margin="2"
IsVisible="{Binding IsDownloadable}" />
Решение второе : если Xaml , то код:
<ListView x:Name="listviewbig" ItemsSource="{Binding Monkeys}"
HasUnevenRows="true"
ItemTapped="OnListViewItemTapped"
ItemSelected="OnListViewItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Text="{Binding Location}"
VerticalOptions="End"
Style="{DynamicResource SubtitleTextStyle}" />
<Button Grid.Row="0"
Grid.Column="1"
Text="Click"
HorizontalOptions="End"
IsVisible="{Binding IsDownloadable}"
Clicked="OnClicked"
Command="{Binding DownloadCommand}"
CommandParameter="2"/>
<ProgressBar Grid.Row="1"
Grid.Column="3" x:Name="progressBar"
Progress="{Binding CurrentProgress,Mode=OneWayToSource}"
PropertyChanged="ProgressBar_PropertyChanged"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
MonkeysViewModel.cs
class MonkeysViewModel
{
public IList<Monkey> Monkeys { get; private set; }
private async void ShowCustomerInfo(object obj)
{
Console.WriteLine("true" + obj );
}
public MonkeysViewModel()
{
Monkeys = new List<Monkey>();
//f = new F[5];
Monkeys.Add(new Monkey
{
Name = "Baboon",
Location = "Africa & Asia",
CommandShowInfo = new Command(ShowCustomerInfo),
IsDownloadable = true
});
...
}
}
}
Monkey.cs
public class Monkey : INotifyPropertyChanged
{
public string Name { set; get; }
public string Location { get; set; }
public System.Windows.Input.ICommand CommandShowInfo { get; set; }
private bool isDownloadable = true;
public bool IsDownloadable
{
set
{
if (isDownloadable != value)
{
isDownloadable = value;
OnPropertyChanged("IsDownloadable");
}
}
get
{
return isDownloadable;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Здесь ListView ContentPage: BindingContext = new MonkeysViewModel();
и метод тестирования в кнопке:
void OnClicked(Object sender, MyEventArgs args)
{
Button btn = sender as Button;
var monkey = btn.BindingContext as Monkey;
monkey.IsDownloadable = false;
}
Возможно, проблема связана с привязкой. Неправильно используется модель.