【问题标题】:Xamarin Forms ignore Button binding from DataType on StackLayoutXamarin Forms 忽略 StackLayout 上 DataType 的 Button 绑定
【发布时间】:2021-01-12 05:43:12
【问题描述】:

我有以下 xaml 代码:

            <StackLayout Padding="10" x:DataType="model:Light">
                    <StackLayout HorizontalOptions="FillAndExpand" Grid.Column="0" Grid.Row="1">
                        <Label Text="{Binding Name}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="24" HorizontalOptions="StartAndExpand"  VerticalOptions="StartAndExpand"/>
                    </StackLayout>
                    <StackLayout HorizontalOptions="FillAndExpand" Grid.Column="1" Grid.Row="1" >
                        <Button Text="{Binding Enabled}" Command="{Binding ToggleLight}" CommandParameter="{Binding .}" HorizontalOptions="CenterAndExpand"></Button>
                    </StackLayout>
            </StackLayout>

如何在我的 StackLayout 上定义的 x:DataType="model:Light" 中排除我的 Button 上的 Command="{Binding ToggleLight}"

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    使用binding-path

    1. 在 StackLayout(StackLayout、Grid、ListView 等)之外定义“x:Name”
    2. 在 StackLayout 中为您的 Command 绑定添加“x:Reference”
    3. 命令被排除。

    喜欢:

    <Grid x:Name="WhateverParent">
        <StackLayout x:DataType="model:Light">
            <Button ... Command="{Binding Path=BindingContext.ToggleLight, Source={x:Reference WhateverParent}}" />
        </StackLayout>
    </Grid>
    

    【讨论】:

    • 谢谢!我对其进行了一些修改,以便可以将我的对象发送到我的 ViewModel:&lt;Button Text="{Binding Enabled}" Command="{Binding Source={RelativeSource AncestorType={x:Type local:LightsViewModel}}, Path=ToggleLight}" CommandParameter="{Binding .}" HorizontalOptions="CenterAndExpand"/&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2014-10-27
    相关资源
    最近更新 更多