WPF中的绑定问题

时间:2014-04-19 14:52:37

标签: c# wpf binding

我有绑定问题,我试图移动鼠标移动鼠标时,这是我的清单:

    <ListBox x:Name="icTables" FlowDirection="LeftToRight" ItemsSource="{Binding Path=ocTablesinSection,UpdateSourceTrigger=PropertyChanged}" Margin="0,101,52,0" Grid.Column="1" SelectionMode="Extended" HorizontalAlignment="Right" Width="705" Height="400" VerticalAlignment="Top">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style
                TargetType="ListBoxItem">
                <Setter Property="Canvas.Left" Value="{Binding Path=CLeft,UpdateSourceTrigger=PropertyChanged}"/>
                <Setter Property="Canvas.Top" Value="{Binding Path=CTop,UpdateSourceTrigger=PropertyChanged}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Rectangle Width="50" Height="50" Fill="Red" Cursor="Hand" MouseDown="Rectangle_MouseDown" MouseUp="Rectangle_MouseUp" MouseMove="Rectangle_MouseMove" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在代码背后的MouseMove事件中:

        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDraggingRectangle)
        {
            //
            // Drag-move selected rectangles.
            //
            Point curMouseDownPoint = e.GetPosition(this);
            var dragDelta = curMouseDownPoint - origMouseDownPoint;

            origMouseDownPoint = curMouseDownPoint;

            foreach (var item in ocTablesinSection)
            {
                item.CLeft += dragDelta.X;
                item.CTop += dragDelta.Y;
            }
       }
        else if (isLeftMouseDownOnRectangle)
        {
            //
            // The user is left-dragging the rectangle,
            // but don't initiate the drag operation until
            // the mouse cursor has moved more than the threshold value.
            //
            Point curMouseDownPoint = e.GetPosition(this);
            var dragDelta = curMouseDownPoint - origMouseDownPoint;
            double dragDistance = Math.Abs(dragDelta.Length);
            if (dragDistance > DragThreshold)
            {
                //
                // When the mouse has been dragged more than the threshold value commence dragging the rectangle.
                //
                isDraggingRectangle = true;
            }

            e.Handled = true;
        }
    }

每件事都有效,但UI没有显示更新(矩形不移动),当我将CLeft和CTop更改为公共变量而窗口作为元素名称时,它可以工作!!

我的代码中有什么问题阻止矩形移动鼠标?

//更新 -

private ObservableCollection<TablesinSection> _ocTablesinSection;

public ObservableCollection ocTablesinSection         {             get {return _ocTablesinSection; }             设置{                 _ocTablesinSection = value;                 OnPropertyChanged( “ocTablesinSection”);                 }         }

1 个答案:

答案 0 :(得分:2)

WPF绑定不适用于字段。它仅适用于 properties

将它们声明为属性。

public double CLeft { get; set; }
public double CTop { get; set; }

如果您想要更新任何属性更改的UI,您必须在包含此属性的类上实现INotifyPropertyChanged接口。


<强>更新

设置属性以引发PropertyChangedEvent

private double cLeft;
public double CLeft
{
   get
   {
      return cLeft;
   }
   set
   {
      if(cLeft != value)
      {
         cLeft = value;
         OnPropertyChanged("CLeft");
      }
   }
}

CTop 也这样做。