【问题标题】:Binding Text Not showing in xamarin forms绑定文本未以 xamarin 形式显示
【发布时间】:2018-12-11 18:20:07
【问题描述】:

在按钮上添加 BindingContext 后,内部列表项按钮 ActionText 未显示。 从 Button 中删除 BindingContext 后,ActionText 显示,但按钮单击事件不起作用。

<ListView x:Name="ShipmentData" 
    HasUnevenRows="True" ItemsSource="{Binding ShipmentData}" SelectedItem="{Binding SelectedShippedItem}"
    BackgroundColor="White">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Margin="0" Padding="5" x:Name="Item">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Label Text="{Binding ShipTransId}" Grid.Row="0" IsVisible="False"/>
                        <Label Text="{Binding LabelUri}" Grid.Row="0" IsVisible="False"/>
                        <Label Text="{Binding OrderNumber}" Grid.Row="0" Grid.Column="0" HorizontalTextAlignment="Center"/>
                        <Label Text="{Binding ShippingId}" Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="Center"/>
                        <Label Text="{Binding Status}" Grid.Row="0" Grid.Column="2" 
                               HorizontalTextAlignment="Center" HorizontalOptions="Center" VerticalOptions="Center"/>
                        <Button Text="{Binding ShipmentData.ActionText}" VerticalOptions="Center" 
                        HorizontalOptions="Center" BorderRadius="10"
                        Command="{Binding PrintLabel}" Grid.Row="0" Grid.Column="3" Margin="5,0,0,0"
                        BindingContext="{Binding Source={x:Reference ShipmentData}, Path=BindingContext}"
                        CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.Behaviors>
             <eventToCommand:EventToCommandBehavior EventName="ItemTapped" Command="ShippedItemTapped"/>
        </ListView.Behaviors>
</ListView>

这是我的页面模型

public ObservableCollection<ShowShipmentData> ShipmentData
{
    get { return _shipmentData; }
    set
    {
        _shipmentData = value;
        RaisePropertyChanged();
    }
}

ShipmentData = DependencyService.Get<ISQLite2().ShowShipmentTableData(GlobalVariable.BranchId); 

public Command PrintLabel
{
    get
    {
        return new Command(async (e) =>
        {
            ShowShipmentData selecedItem = (e as ShowShipmentData);
        });
    }
}

如何解决?

【问题讨论】:

  • 我们也需要您在页面模型中的相关代码。
  • 您好,我编辑了我的 qus,请看这个。
  • DependencyService.Get
  • ActionText 是 ShowShipmentData 的属性吗?那么它应该是:&lt;Button Text="{Binding ActionText}"
  • ActionText 是 ShowShipmentTableData 类和 DependencyService 的属性。Get

标签: xamarin xamarin.forms


【解决方案1】:

尝试如下修改您的 Xaml 代码,

删除以下项目

Command="{Binding PrintLabel}" BindingContext="{Binding Source={x:Reference ShipmentData}, Path=BindingContext}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"

并使用以下代码进行更新,为您的页面命名并在“命令”中提及

 Command="{Binding Source={x:Reference yourPageName}, Path=BindingContext.PrintLabel}"
                                                        CommandParameter="{Binding .}"

在后面的代码中,更改您的 PrintLabel 命令属性,如下所示,

Command printLabel;
    public Command PrintLabel
    {
        get
        {
            return printLabel ?? (printLabel = new Command<ShowShipmentData>(ExecuteSelected));
        }
    }

    private void ExecuteSelected(ShowShipmentData selectedItem)
    {

    }

【讨论】:

  • 对于按钮文本,只需按照 Gerald Versluis 的建议使用 Text="{Binding ActionText}
  • Command="{Binding Source={x:Reference yourPageName}, Path=BindingContext.PrintLabel}" 当我放置 yourPageName 时,它​​不起作用,但放置 MyListview x:Name 后它成功了。谢谢: )
  • 太棒了.. 欢迎 :-)
猜你喜欢
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2018-06-22
  • 1970-01-01
  • 2020-07-28
相关资源
最近更新 更多