[英]Xamarin Forms Button Command binding inside a ListView
我有以下問題,在我看來我有一個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>
在我的ViewModel中是我需要的一切,我已經使用不在listview中的按鈕測試了命令,它完美無缺。
如果我將鼠標懸停在綁定上,則會顯示消息“無法解析符號'...'”
一月,
由於您已使用列表視圖並且您的命令位於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>
因此,您設置綁定源以引用列表視圖的綁定上下文(即您的視圖模型或“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
});
}
}
那是因為你綁定到你的EintragList
一個項 - 屬性(這就是你綁定到文本屬性如Beginn
和Ende
)。 命令綁定嘗試從列表中的單個項目中獲取命令,而不是從您的viewmodel中。
選項1:您在項目類中設置命令並在那里處理點擊。
選項2:告訴您綁定源應該是您的頁面(而不是單個項目):
Command="{Binding BindingContext.EditEintragCommand, Source={x:Reference Name=MyPageName}}"
請確保您的頁面根元素的x:Name="MyPageName"
設置為x:Name="MyPageName"
要知道哪個項觸發了命令,您可以設置CommandParameter屬性,然后將其作為對象發送到命令:
CommandParameter="{Binding .}"
另外:當您使用外部模板顯示列表中的項目時,您可以嘗試我在另一個答案中描述的內容 (相同的原則)。
如果你想綁定Button click,你也可以嘗試在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>
和代碼方面你可以簡單地用一個參數來實現delete方法
public void Delete(Object Sender, EventArgs args)
{
Button button = (Button)Sender;
string ID = button.CommandParameter.ToString();
// Do your Stuff.....
}
這是另一件可以讓你驚訝的事情。 如果您不小心將ViewModel中的ICommand定義為私有屬性,則永遠不會發生對命令的綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.