【问题标题】:Binding Issue in WPFWPF 中的绑定问题
【发布时间】:2014-06-03 23:46:05
【问题描述】:

我的绑定有问题,我试图在移动鼠标时移动矩形,这是我的列表:

    <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 { 得到 { 返回 _ocTablesinSection; } 放 { _ocTablesinSection = 值; OnPropertyChanged("ocTablesinSection"); } }

【问题讨论】:

  • When I change CLeft and CTop to a public variable and the window as Element-name it works !! - 这是什么意思? CLeftCTop 的邮政编码。
  • @Rohit Vats ocTablesinSection 是餐厅表的 Observable 集合,CLeft 和 CTop 是每个记录中描述每个表位置的字段,而不是使用 CLeft 和 CTop 如果我定义了一个公共变量和将其绑定到 Canvas 即可。希望这清楚。

标签: c# wpf binding


【解决方案1】:

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 也这样做。

【讨论】:

  • 感谢您的时间,但是 CLeft 和 CTop 每个都是 TablesinSection 类中的一个属性,在我的项目模型中定义,它应该可以工作还是我错了?
  • 在您提到的 cmets 部分下,这些是字段。无论如何,如果它们是属性,无论何时设置这些值,您是否都会提出 PropertyChangedEventTablesinSection 应该像上面提到的那样实现INPC
  • 是的,我已经将 INPC 实现为:private ObservableCollection _ocTablesinSection;公共 ObservableCollection ocTablesinSection { get { return _ocTablesinSection; } 设置 { _ocTablesinSection = 值; OnPropertyChanged("ocTablesinSection"); } }
  • 使CLeftCTop 遵循相同的语法。
猜你喜欢
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多