很遗憾,xaml 不支持多个绑定。
如果按钮在ListView,
BindingContext="{Binding Source={x:Reference icaoDownloads}, Path=BindingContext}"
应该删除表单按钮。
解决方案一:试试下面的代码:
<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 内容页面:BindingContext = new MonkeysViewModel();
并在按钮单击方法中进行测试:
void OnClicked(Object sender, MyEventArgs args)
{
Button btn = sender as Button;
var monkey = btn.BindingContext as Monkey;
monkey.IsDownloadable = false;
}
也许问题是绑定模型没有正确使用。