更新视图时,ebservableCollection的属性发生了
我有XAML代码
<ListView x:Name="ListObject"
ItemsSource="{x:Bind ObjectList}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid BorderThickness="{Binding BorderThickness}">
</Grid>
</DataTemplate>
</ListView.ItemTemplate></ListView>
代码:
private readonly ObservableCollection<Item> ObjectList = new();
public class Item
{
public Thickness BorderThickness { get; set; }
}
当我进行obsectlist.add(new Item(){borderThickness = new(10)})
时,它将按照预期创建一个带有borderThickness = 10的网格。现在,我想将项目的边框厚度更改为100,我做objectList [0] .borderthickness = new(100)
,但它不起作用,视图未更新。
因此,我的问题是如何在观测值和更新视图中更改项目的边界厚度?
谢谢。
I have xaml code
<ListView x:Name="ListObject"
ItemsSource="{x:Bind ObjectList}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid BorderThickness="{Binding BorderThickness}">
</Grid>
</DataTemplate>
</ListView.ItemTemplate></ListView>
Code-behind:
private readonly ObservableCollection<Item> ObjectList = new();
public class Item
{
public Thickness BorderThickness { get; set; }
}
when I do ObjectList.Add(new Item(){BorderThickness = new(10)})
, It will create a grid with borderthickness = 10 as expected. Now I want to change the border thickness of item to 100, I do ObjectList[0].BorderThickness =new(100)
, but it doesn't work, the view isn't updated.
So, my question is how do I change the item's border thickness in ObservableCollection and update to the view?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
项目
类必须实现InotifyPropertyChanged,并在厚度变化时提高事件。例如:然后,请确保将绑定模式设置为Oneway,因为默认情况下它是一遍的。
Your
Item
class must implement INotifyPropertyChanged, and raise the event when the value of Thickness changes. For example:Then, make sure to set the mode of the binding to OneWay, because by default it is OneTime.