【发布时间】: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 !!- 这是什么意思?CLeft和CTop的邮政编码。 -
@Rohit Vats ocTablesinSection 是餐厅表的 Observable 集合,CLeft 和 CTop 是每个记录中描述每个表位置的字段,而不是使用 CLeft 和 CTop 如果我定义了一个公共变量和将其绑定到 Canvas 即可。希望这清楚。