C# WPF中的绑定问题
我在绑定方面有问题,我试图在移动鼠标时移动矩形,这是我的列表:C# WPF中的绑定问题,c#,wpf,binding,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" HorizontalAl
<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>
当我将CLIFT和CTop更改为公共变量并将窗口更改为元素名称时,一切都正常,但UI不显示更新(矩形不移动)
我的代码中有什么问题阻止矩形用鼠标移动
//更新--
private observeablecollection\u ocTablesinSection;
公共可观测采集八倍频段
{
获取{return\u ocTablesinSection;}
集合{
_ocTablesinSection=值;
关于财产变更(“八分之一财产”);
}
}WPF绑定不适用于字段。它仅适用于
属性
将它们声明为属性
public double CLeft { get; set; }
public double CTop { get; set; }
另外,若要在任何属性更改时更新UI,则必须在包含此属性的类上实现接口
更新 将属性设置为引发
PropertyChangedEvent
:
private double cLeft;
public double CLeft
{
get
{
return cLeft;
}
set
{
if(cLeft != value)
{
cLeft = value;
OnPropertyChanged("CLeft");
}
}
}
对CTop执行同样的操作。
当我将CLIFT和CTop更改为公共变量并将窗口更改为元素名称时,它会工作代码>-你这是什么意思?CLeft
和CTop
@Rohit-Vats-octablesinssection是一个可观察的Resturant表集合,CLeft和CTop是每个记录中描述每个表位置的字段,而不是使用CLeft和CTop,如果我已经定义了一个公共变量并将其绑定到画布上,它就可以工作。希望这一点很清楚。谢谢你们,但CLeft和CTop都是在我的项目模型中定义的TablesinSection类中的一个属性,它应该可以工作,否则我错了?在评论部分你们提到了这些字段。不管怎样,如果它们是属性,那么每当设置这些值时,您都会引发PropertyChangedEvent
<代码>表安全设置
应该实现上面提到的INPC
;公共OctableCollection OctablesSection{get{return{uOctablesSection;}set{uOctablesSection=value;OnPropertyChanged(“OctablesSection”);}使CLeft
和CTop
遵循相同的语法。
public double CLeft { get; set; }
public double CTop { get; set; }
private double cLeft;
public double CLeft
{
get
{
return cLeft;
}
set
{
if(cLeft != value)
{
cLeft = value;
OnPropertyChanged("CLeft");
}
}
}