【问题标题】:Can you have multiple bindings on one button?您可以在一个按钮上进行多个绑定吗?
【发布时间】:2019-03-28 22:57:21
【问题描述】:

我正在尝试在列表视图中的一个按钮上设置命令绑定和 IsVisible 绑定。如果我在按钮中有绑定上下文,则命令绑定将起作用,而 IsVisible 绑定将不起作用。如果我删除绑定上下文,IsVisible 绑定将起作用,但命令绑定将不起作用。有没有人对如何补救这种情况提出建议。

<Button BindingContext="{Binding Source={x:Reference icaoDownloads}, 
        Path=BindingContext}"
        Command="{Binding DownloadCommand}"
        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}" />

我有一个从数据库集创建的列表。我试图仅在数据库有可用更新时显示按钮。所以我想让按钮仅在有更新时出现,单击按钮并将数据下载到数据库,然后使按钮不可见。

【问题讨论】:

  • 你能显示模型的代码被绑定了吗?
  • 如何管理 IsDownloadable 绑定?请也发布该代码。
  • 这里说的多重绑定到底是什么意思?
  • 我试图为按钮绑定一个命令并将 isVisible 属性绑定到另一个命令。我找到了解决办法。
  • 我在下面添加了一个解决方案以供参考。

标签: xaml xamarin.forms binding


【解决方案1】:

很遗憾,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;
}

也许问题是绑定模型没有正确使用。

【讨论】:

  • 如果我正确阅读了问题,这应该可以解决问题。除此之外,似乎有人尝试将当前项目绑定到命令参数。要绑定整个当前的 BindingContext,您可以使用 {Binding .}
  • @Knoop 是的,如果解决了这个问题。记得标记,提前谢谢。^.^
  • 江 我不是问这个问题的人,只是评论了一个改进。
  • @Knoop Okey ,知道了。感谢您的改进,不客气。 *.^
【解决方案2】:

这是我的解决方案:

<Button CommandParameter="{Binding .}"
                                            Command="{Binding BindingContext.DownloadCommand, Source={x:Reference TestIcaoContent}}"
                                            HorizontalOptions="CenterAndExpand"
                                            VerticalOptions="Center"
                                            BackgroundColor="Black"
                                            TextColor="WhiteSmoke"
                                            Grid.Column="1"
                                            Text="  Update  "
                                            BorderColor="DarkSlateGray"
                                            BorderRadius="5"
                                            BorderWidth="2"
                                            Margin="2"
                                            IsVisible="{Binding Source={x:Reference Item}, Path=BindingContext.IsDownloadable}" />

这解决了我的问题,让我将列表项值传递给视图模型。

这是我的视图模型构造函数:

public ICommand NavCommandCommand { get; private set; }
public INavigation Navigation { get; set; }
public ICommand DownloadCommand { get; private set; }

        public TestIcaoViewModel()
        {

            //DownloadCommand = new Command(Download);
            DownloadCommand = new Command((e) => { var item = (e as TestLandingViewModel); Download(item.IcaoCode); });

            NavCommandCommand = new Command((e) => { var item2 = (e as TestLandingViewModel); NavCommand(item2.IcaoCode); });
            LoadAirportData();   
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2015-02-28
    • 2012-06-12
    相关资源
    最近更新 更多