在数据模板中绑定命令 (XF/Prism)
Binding a Command within a Datatemplate (XF/Prism)
使用 Prism for Xamarin Forms,我有一个像这样的 ListView:
<ListView ItemsSource="{Binding Conditions}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="4" RowSpacing="0">
<Button Command="{Binding DoSomething}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
其中 Conditions 是 ConditionViewModel 的 ObservableCollection,DoSomething 命令位于页面的 ViewModel 中。
当然,绑定在这里不起作用,因为它的绑定上下文将成为一个单独的 ConditionViewModel 项。
我知道相对绑定在 Xamarin 中不可用,规定的解决方案似乎是直接应用 {x:Reference SomeViewModel}。但是使用 Prism,View 无法直接访问其 ViewModel,因此这是不可能的。
我也看过这个,试图获得 RelativeContext 标记扩展:
https://github.com/corradocavalli/Xamarin.Forms.Behaviors/blob/master/Xamarin.Behaviors/Extensions/RelativeContextExtension.cs
但是,得到一个异常,即 RootObject 为空。
还有其他解决方案吗?
如果我没看错你的问题,你想要的是这样的:
视图模型:
public class MyPageViewModel : BindableBase
{
public MyPageViewModel()
{
MyCommand = new DelegateCommand<MyModel>( ExecuteMyCommand );
}
public ObservableCollection<MyModel> Collection { get; set; }
public DelegateCommand<MyModel> MyCommand { get; }
private void ExecuteMyCommand( MyModel model )
{
// Do Foo
}
}
查看;
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Views.MyPage"
x:Name="page">
<ListView ItemsSource="{Binding Collection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Button Command="{Binding BindingContext.MyCommand,Source={x:Reference page}}"
CommandParameter="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
使用 Prism for Xamarin Forms,我有一个像这样的 ListView:
<ListView ItemsSource="{Binding Conditions}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="4" RowSpacing="0">
<Button Command="{Binding DoSomething}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
其中 Conditions 是 ConditionViewModel 的 ObservableCollection,DoSomething 命令位于页面的 ViewModel 中。
当然,绑定在这里不起作用,因为它的绑定上下文将成为一个单独的 ConditionViewModel 项。
我知道相对绑定在 Xamarin 中不可用,规定的解决方案似乎是直接应用 {x:Reference SomeViewModel}。但是使用 Prism,View 无法直接访问其 ViewModel,因此这是不可能的。
我也看过这个,试图获得 RelativeContext 标记扩展: https://github.com/corradocavalli/Xamarin.Forms.Behaviors/blob/master/Xamarin.Behaviors/Extensions/RelativeContextExtension.cs 但是,得到一个异常,即 RootObject 为空。
还有其他解决方案吗?
如果我没看错你的问题,你想要的是这样的:
视图模型:
public class MyPageViewModel : BindableBase
{
public MyPageViewModel()
{
MyCommand = new DelegateCommand<MyModel>( ExecuteMyCommand );
}
public ObservableCollection<MyModel> Collection { get; set; }
public DelegateCommand<MyModel> MyCommand { get; }
private void ExecuteMyCommand( MyModel model )
{
// Do Foo
}
}
查看;
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Views.MyPage"
x:Name="page">
<ListView ItemsSource="{Binding Collection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Button Command="{Binding BindingContext.MyCommand,Source={x:Reference page}}"
CommandParameter="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>