1

I want to dispalay some byte values and their binary values.like this:My 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

<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.

If i use bool type not bool[] type,it workes.

snieguu
  • 2,073
  • 2
  • 20
  • 39
heater
  • 11
  • 2

1 Answers1

0

First of all, your MyRegs class does not implement INotifyPropertyChanged, so your UI elements have no way of knowing when your values get changed. You must implement that interface on your class and call NotifyPropetyChanged in the appropriate setters.

Second, ObservableCollection only sends update notifications when items are added or removed from the collection, not when items' properties have been updated.

Smolakian
  • 414
  • 3
  • 15
  • 1
    A BindingList would do both, inser/delete and modify notifications – Holger Nov 18 '19 at 14:26
  • BindingList was designed for WinForms and is not properly supported for WPF. See: https://stackoverflow.com/questions/9532629/why-not-bindinglist-in-wpf – Smolakian Nov 18 '19 at 18:58
  • First of all, I think NotifyPropertyChanged doesn't matter with the Set Accessor.In the fact,RegValue can be seted when I edit the TextBox. – heater Nov 19 '19 at 01:40
  • Second, ObservableCollection only sends update notifications when items are added or removed from the collection, not when items' properties have been updated.----- I agree. – heater Nov 19 '19 at 02:34
  • Back to my question,it doesn't go to the Setter when I check those CheckBoxs.How to explain this,maybe the binding doesn't work.How can I fix it. – heater Nov 19 '19 at 02:38