4

私が抱えている問題を示すために、小さな例を作成しました。

最初に私のクラス:


public class DisplayRow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int?[] values;
    private string title;

    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
        }
    }

    public int?[] Values
    {
        get { return values; }
        set
        {
            values = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Values[]"));
        }
    }

    public DisplayRow()
    {
        Values = new int?[6];
    }
}

問題は配列であるため、Values プロパティです。配列内の要素が更新されたときに INotifyPropertyChanged を正しく呼び出す方法がわかりません。

ここに私のxamlがあります:

<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox x:Name="MyListBox" Margin="0,0,0,65">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Title}" />
                        <TextBlock Text="{Binding Path=Values[0]}" Margin="5,0,0,0" />
                        <TextBlock Text="{Binding Path=Values[1]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[2]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[3]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[4]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[5]}" Margin="5,0,0,0"  />                        
                    </WrapPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Height="23" Margin="27,0,0,23" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="74" Click="button1_Click">Button</Button>
    </Grid>
</Window>

そして背後にあるコード:

public partial class Window1 : Window
{
    private readonly ObservableCollection<DisplayRow> displayRows = new ObservableCollection<DisplayRow>();

    public Window1()
    {
        InitializeComponent();

        displayRows.Add(new DisplayRow {Title = "Item 1", Values = new int?[] {1, 2, 3, 4, 5, 6}});
        displayRows.Add(new DisplayRow {Title = "Item 2", Values = new int?[] {7, 8, 9, 10, 11, 12}});
        displayRows.Add(new DisplayRow {Title = "Item 3", Values = new int?[] {13, 14, 15, 16, 17, 18}});

        MyListBox.ItemsSource = displayRows;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        foreach (DisplayRow row in displayRows)
        {
            row.Values[0] = 99;
        }
    }
}

ボタンをクリックすると、最初の行の値が変更されますが、その変更は UI に反映されません。Title プロパティを変更すると、タイトルが正しく更新されます。

INotifyPropertyChanged を呼び出して、配列要素が更新されたことを理解する方法はありますか?

4

2 に答える 2

10

既存のコードが機能しない理由は、配列全体をプロパティに再割り当てするのではなく、配列内の値を変更しているためです。したがって、プロパティ変更イベントを発生させることはありません。

コレクション自体の変更をよりよく理解するために、WPF バインディング インフラストラクチャがフックする INotifyCollectionChanged を実装するObservableCollectionを代わりに使用します。

于 2009-10-21T16:58:35.780 に答える
2

配列の代わりに を使用して、ObservableCollection<int?>すべての配管を行うことができます。

別のオプションは、int?コンテナーを作成し、それらで配列を埋め、そのセッターPropertyChangedでイベントを発生させることです。int?

于 2009-10-21T17:01:22.607 に答える