简体   繁体   English

为什么我不能在 ObservableCollection 中使用数组值进行绑定?

[英]Why can't I use array value in a ObservableCollection for binding?

I want to dispalay some byte values and their binary values.like this: My UI .我想显示一些字节值和它们的二进制值。像这样:我的 UI

my coding: this is the item source:我的编码:这是项目来源:

public MainWindow()
    {
        InitializeComponent();

        ObservableCollection<MyRegs> regs = new ObservableCollection<MyRegs>()
        {
            new MyRegs(0x12),
            new MyRegs(0x23),
            new MyRegs(0x34)
        };
        RegsList.ItemsSource = regs;
    }

this is custom data:这是自定义数据:

public class MyRegs
{
    public MyRegs(byte val)
    {
        this.RegValue = val;

        this.Bits = new bool[] { true, false, true, false, true, false, true, false };
    }

    private byte regValue;
    public byte RegValue
    {
        get { return regValue; }
        set
        {
            regValue = value;
        }
    }

    private bool[] bits;
    public bool[] Bits
    {
        get
        {
            return bits;
        }
        set
        {
            bits = value;
        }
    }
}

Xaml: by the way, Can I use a template to get those checkboxes Xaml:顺便问一下,我可以使用模板来获取这些复选框吗

<Grid>
<ListView x:Name="RegsList">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBox  Text="{Binding Path=RegValue,Mode=TwoWay, Converter={StaticResource sResource1}}" HorizontalAlignment="Left" TextWrapping="Wrap"/>
                <CheckBox IsChecked="{Binding Path=Bits[7],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[6],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[5],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[4],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[3],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[2],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[1],Mode=TwoWay}"/>
                <CheckBox IsChecked="{Binding Path=Bits[0],Mode=TwoWay}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

but when I check the CheckBox,it doesn't set Bits value.Why???但是当我检查复选框时,它没有设置位值。为什么???

I haved used the ObservableCollection,it didn't work too.我已经使用了 ObservableCollection,它也没有用。

If i use bool type not bool[] type,it workes.如果我使用 bool 类型而不是 bool[] 类型,它可以工作。

First of all, your MyRegs class does not implement INotifyPropertyChanged, so your UI elements have no way of knowing when your values get changed.首先,您的MyRegs class 没有实现 INotifyPropertyChanged,因此您的 UI 元素无法知道您的值何时更改。 You must implement that interface on your class and call NotifyPropetyChanged in the appropriate setters.您必须在 class 上实现该接口,并在适当的设置器中调用NotifyPropetyChanged

Second, ObservableCollection only sends update notifications when items are added or removed from the collection, not when items' properties have been updated.其次,ObservableCollection 仅在从集合中添加删除项目时发送更新通知,而不是在项目的属性已更新时发送更新通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM