添加到observable集合时更新UI

时间:2012-03-10 14:49:02

标签: wpf drag-and-drop observablecollection

iv'e有两个物品控制绑定到2个可观察的集合

my ItemsControls:

    <ItemsControl Grid.Column="4"  Name="Pipe19"  ItemsSource="{Binding Path=Pipes[19].Checkers}" Style="{StaticResource ItemsControlStyle}" ItemsPanel="{StaticResource TopPipePanelTemplate}" />
    <ItemsControl Grid.Column="5"  Name="Pipe18"  ItemsSource="{Binding Path=Pipes[18].Checkers}" Style="{StaticResource ItemsControlStyle}" ItemsPanel="{StaticResource TopPipePanelTemplate}" />

他们的风格定义:

   <Style TargetType="{x:Type ItemsControl}" x:Key="ItemsControlStyle">
        <Setter Property="ItemTemplate" Value="{StaticResource PipeDataItem}"></Setter>
        <Setter Property="AllowDrop" Value="True"></Setter>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ItemsControl_MouseLeftButtonDown"></EventSetter>
         <EventSetter Event="Drop" Handler="ItemsControl_Drop"></EventSetter>
   </Style> 

两者都被认为是掉落目标,它们包含我需要从一个拖到另一个的椭圆项。问题是操作的添加部分没有呈现给UI。

视觉助手:

enter image description here

当拖拽从右边向左拖动为椭圆时:

这是抓取椭圆并保存源代码控制的代码

    private void ItemsControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ItemsControl control = (ItemsControl)sender;
        UIElement element = control.InputHitTest(e.GetPosition(control)) as UIElement;

        if (element != null)
        {
            Ellipse ellipse = element as Ellipse;
            DragSource = control;
            DragDrop.DoDragDrop(ellipse, ellipse, DragDropEffects.Copy);
        }
    }

这是将椭圆放到目标上的代码:

    private void ItemsControl_Drop(object sender, DragEventArgs e)
    {
        ItemsControl target = (ItemsControl)sender;
        Ellipse ellipse = (Ellipse)e.Data.GetData(typeof(Ellipse));
        ((ObservableCollection<Checker>)DragSource.ItemsSource).Remove(ellipse.DataContext as Checker); 
        ((ObservableCollection<Checker>)target.ItemsSource).Add(ellipse.DataContext as Checker); 
    } 

Checkers系列描述如下:(注意它们是itemscontrols ItemsSource:

public class Pipe  
{    
    private ObservableCollection<Checker> checkers;
    public ObservableCollection<Checker> Checkers
    {
        get 
        {
            if (checkers == null)
                checkers = new ObservableCollection<Checker>();
            return checkers; 
        }            
    }        
}

在ItemsControl_Drop事件之后,结果是只有删除更新了UI,但是目标上的添加没有添加(我希望左边的一个新项目会出现在我的名为Add on it的itemsource:

另一种视觉辅助:

enter image description here

任何想法?

3 个答案:

答案 0 :(得分:1)

也许问题是你添加到ItemsControl的ItemsSource而不是直接添加到Pipes [#]。Checkers。 我注意到remove方法是从DragSource.ItemsSource中删除的。 您可能有一个DragTargetControl,您应该添加ItemsSource,这将更新Binding。 如果你将Binded ItemsSource强制转换为ObservableCollection,你可能正在撤消Binding。

答案 1 :(得分:1)

我想我知道发生了什么。我敢打赌Pipes []不是ObservableCollection。尝试使用Pipe1和Pipe2。从Pipe1中删除并添加到Pipe2。

答案 2 :(得分:0)

如果你删除了ui元素,它会失去它的引用 从一个可观察的集合中添加到另一个之前,

所以我将椭圆添加到目标,然后将其从源中删除。

    private void ItemsControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ItemsControl control = (ItemsControl)sender;
        UIElement element = control.InputHitTest(e.GetPosition(control)) as UIElement;

        if (element != null && (element is Ellipse))
        {
            Ellipse ellipse = element as Ellipse;
            DragSource = control;
            DragDrop.DoDragDrop(ellipse, ellipse, DragDropEffects.Move);
        }
    }

    private void ItemsControl_Drop(object sender, DragEventArgs e)
    {
        ItemsControl target = (ItemsControl)sender;
        Ellipse ellipse = (Ellipse)e.Data.GetData(typeof(Ellipse));
        ObservableCollection<Checker> collection = target.ItemsSource as ObservableCollection<Checker>;
        Checker checker = ellipse.DataContext as Checker;
        collection.Add(checker);
        ((ObservableCollection<Checker>)DragSource.ItemsSource).Remove(ellipse.DataContext as Checker);

    }