Jan*_*Mer 24 c# xaml listview xamarin.forms
我有以下问题,在我看来我有一个Listview.在这个列表视图中,我想有两个按钮.一个用于编辑项目,一个用于删除它.
这是我在XAML中的列表视图
<ListView Grid.Row="1" x:Name="ArbeitsEinträgeList" ItemsSource="{Binding EintragList}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Titel}" TextColor="{Binding Fehlerhaft, Converter={StaticResource EintragartConverter}}"></Label>
<Label Grid.Column="1" Text="{Binding Beginn}" TextColor="{Binding BeginnManuell, Converter={StaticResource EintragartConverter}}"></Label>
<Label Grid.Column="2" Text="{Binding Ende}" TextColor="{Binding EndeManuell, Converter={StaticResource EintragartConverter}}"></Label>
<Button Grid.Column="3" Command="{Binding EditEintragCommand}" Text="" FontFamily="../Ressources/fontawesome.ttf#FontAwesome"></Button>
<Button Grid.Column="4" Command="{Binding DeleteEintragCommand}" Text="" FontFamily="../Ressources/fontawesome.ttf#FontAwesome"></Button>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
在我的ViewModel中是我需要的一切,我已经使用不在listview中的按钮测试了命令,它完美无缺.
如果我将鼠标悬停在绑定上,则会显示消息"无法解析符号'...'"
DPa*_*rry 41
一月,
由于您已使用列表视图并且您的命令位于DataTemplate内部,因此绑定将附加到ItemSource中每个单独模型的绑定上下文.
解决这个问题的方法是做以下事情:
<ListView Grid.Row="1" x:Name="ArbeitsEinträgeList" ItemsSource="{Binding EintragList}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid x:Name="Item">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Titel}" TextColor="{Binding Fehlerhaft, Converter={StaticResource EintragartConverter}}"></Label>
<Label Grid.Column="1" Text="{Binding Beginn}" TextColor="{Binding BeginnManuell, Converter={StaticResource EintragartConverter}}"></Label>
<Label Grid.Column="2" Text="{Binding Ende}" TextColor="{Binding EndeManuell, Converter={StaticResource EintragartConverter}}"></Label>
<Button Grid.Column="3" BindingContext="{Binding Source={x:Reference ArbeitsEinträgeList}, Path=BindingContext}" Command="{Binding EditEintragCommand}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" Text="" FontFamily="../Ressources/fontawesome.ttf#FontAwesome"></Button>
<Button Grid.Column="4" BindingContext="{Binding Source={x:Reference ArbeitsEinträgeList}, Path=BindingContext}" Command="{Binding DeleteEintragCommand}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" Text="" FontFamily="../Ressources/fontawesome.ttf#FontAwesome"></Button>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
因此,您设置绑定源以引用列表视图的绑定上下文(即您的视图模型或"ArbeitsEinträgeList".您还可以将命令参数设置为每个单独项目的绑定上下文.如您所见,我有x:网格上的Name ="Item"和CommandParameter ="{Binding Source = {x:Reference Item},Path = BindingContext}".
简单地说,声明这样的命令允许您在视图模型中定义一个通用命令,并在命令参数执行时使用命令参数作为单个项的绑定上下文.
public ICommand DeleteEintragCommand
{
get
{
return new Command((e) =>
{
var item = (e as MyModelObject);
// delete logic on item
});
}
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*ehl 17
那是因为你绑定到你的EintragList
- 属性中的一个项目(这就是为什么你绑定到文本属性Beginn
和Ende
工作).命令绑定尝试从列表中的单个项目中获取命令,而不是从您的viewmodel中.
选项1:您在项目类中设置命令并在那里处理点击.
选项2:告诉您绑定源应该是您的页面(而不是单个项目):
Command="{Binding BindingContext.EditEintragCommand, Source={x:Reference Name=MyPageName}}"
Run Code Online (Sandbox Code Playgroud)
请确保您设置了页面root-element的名称 x:Name="MyPageName"
要知道哪个项触发了命令,您可以设置CommandParameter属性,然后将其作为对象发送到命令:
CommandParameter="{Binding .}"
Run Code Online (Sandbox Code Playgroud)
另外:当您使用外部模板显示列表中的项目时,您可以尝试我在另一个答案中描述的内容(相同的原则).
如果要绑定Button单击,还可以尝试在Button属性中使用Clicked事件。这是我的代码,它对我有用
<ListView x:Name="lst1" RowHeight="80">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="8,0,8,0">
<Label Text="{Binding Fname}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
<Label Text="{Binding Mobile}" TextColor="#000" LineBreakMode="TailTruncation" />
<Button Text="Remove" Clicked="Delete" CommandParameter="{Binding ID}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
在代码方面,您可以简单地使用诸如以下参数来实现delete方法
public void Delete(Object Sender, EventArgs args)
{
Button button = (Button)Sender;
string ID = button.CommandParameter.ToString();
// Do your Stuff.....
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22169 次 |
最近记录: |