更新视图时,ebservableCollection的属性发生了

发布于 2025-02-03 22:17:47 字数 982 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的项目类必须实现InotifyPropertyChanged,并在厚度变化时提高事件。例如:

class Item : INotifyPropertyChanged
{
    private Thickness borderThickness;

    public Thickness BorderThickness
    {
        get { return borderThickness; }
        set
        {
            if (borderThickness!= value)
            {
                borderThickness= value;
                OnPropertyChanged(nameof(BorderThickness));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后,请确保将绑定模式设置为Oneway,因为默认情况下它是一遍的。

Your Item class must implement INotifyPropertyChanged, and raise the event when the value of Thickness changes. For example:

class Item : INotifyPropertyChanged
{
    private Thickness borderThickness;

    public Thickness BorderThickness
    {
        get { return borderThickness; }
        set
        {
            if (borderThickness!= value)
            {
                borderThickness= value;
                OnPropertyChanged(nameof(BorderThickness));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then, make sure to set the mode of the binding to OneWay, because by default it is OneTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文