我的应用有两个ListBoxes。我需要,当用户从我的ListBox1
抓住某个项目时,相同的项目应该出现在ListBox2
中。
我当前的代码有效,但我需要在保留项目之前选择项目(触摸项目)。我知道原因是我正在使用SelectedItem
,但是如何处理暂停事件?
我的代码:
private void holdListAdd(object sender, System.Windows.Input.GestureEventArgs e)
{
if (List1.SelectedItem != null)
{
Fields fi = (Fields)this.List1.SelectedItem;
// fi.Quantity = txtQuantity.Text;
if (List2.Items.Contains(List1.SelectedItem))
{
MessageBox.Show("Esse item já foi adicionado!");
}
else
{
List2.Items.Add(fi);
MessageBox.Show("Item Adicionado com sucesso!");
}
}
}
我的XAML:
<ListBox Name="List1" ItemsSource="{Binding Items}" Hold="holdListAdd">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="List2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:1)
尝试将事件处理程序附加到ListBox
或ListBoxItem
的{{1}}中的控件,而不是将保留事件处理程序附加到ListBox
,例如:
DataTemplate
然后你可以从<ListBox Name="List1" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Hold="holdListAdd" Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
参数中获取触发hold事件的项目:
sender