0

カスタムクラスを含むユーザーコントロールで作成されたObservableCollectionがあります。カスタムクラスには、いくつかの文字列とブール値が含まれていますが、特別なことは何もありません。

public class StringLineInfo : INotifyPropertyChanged
{
    private string name { set; get; }
    private Color color { set; get; }
    private bool visible { set; get; }
    private bool follow { set; get; }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
        }
    }

    public Color Color
    {
        get { return color; }
        set
        {
            color = value;
            NotifyPropertyChanged("Color");
        }
    }

    public bool Visible
    {
        get { return visible; }
        set
        {
            visible = value;
            NotifyPropertyChanged("Visible");
        }
    }

    public bool Follow
    {
        get { return follow; }
        set
        {
            follow = value;
            NotifyPropertyChanged("Follow");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
}

カスタムコントロールのXaml内に、カスタム凡例コントロールを追加しました。カスタム凡例の中に、ItemsControlがデータバインドされている別のObservableCollectionがあります。

legend.xamlで:

                                <!--<Image Name="imgMyImage" Grid.Column="1" Height="30" Width="30" Margin="5" Source="{Binding Name, Converter=NameToSrcConverter, ConverterParameter={StaticResource IconDictionary}}" />-->
                            <TextBlock Name="txtlineName" FontSize="12" Foreground="Black"  Text="{Binding Converter={StaticResource nameConverter}}"  Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

legend.xaml.cs:

    public ObservableCollection<StringLineInfo> allLines = new ObservableCollection<StringLineInfo>();

    public Dictionary<string, string> DvrIconDictionary = new Dictionary<string, string>();
    public Legend()
    {
        InitializeComponent();
        DataContext = this;
        itmCtrol.ItemsSource = allLines;
    }

    // to set the ItemsSource
    public void setItemsSource(ObservableCollection<StringLineInfo> source)
    {
        if (itmCtrl.ItemsSource == null)
        {
            itmCtrl.ItemsSource = source;
        }
    }

メインユーザーコントロールのコンストラクターで、凡例のObservableCollectionをメインユーザーコントロールのObservableCollectionと同じに設定しました。

public MainControl()
    {
        InitializeComponent();
        MapLegend.SizeChanged += new SizeChangedEventHandler(MapLegend_SizeChanged);
        MapLegend.allLines = this.allLines;
    }

両方のObservableCollectionsに常に同じデータが含まれていても(現在は同じオブジェクトになっています)、ItemsControlは何も更新または表示しません。ただし、コードの後半でObservableCollectionsを同じに設定した場合、たとえばボタンをクリックすると、問題なく機能します。

ボタンは「setItemsSource」関数を呼び出すだけです。

起動時にそれらを等しく設定できない理由について何かアイデアはありますか?

4

1 に答える 1

2

実際には、バインドしようとしているallLinesコレクションへの参照が3つあります。

  1. MainControlメソッドを含むタイプのallLines。
  2. legend.xaml.cs内のallLines。
  3. itmCtrol.ItemsSourcewithlegend.xaml.cs。

最終的にデータが表示されるので、最初の参照が正しく入力されていると思います。2番目の参照は、不要な空のObservableCollectionで初期化されます(このオブジェクトを初期化するオーバーヘッドを節約できます)。3番目は、2番目と同じ空のコレクションに設定されます。

MainControlメソッドのコードは、正しく入力されたコレクションへの2番目の参照を設定しますが、3番目の参照(itmCtrl.ItemsSource)にはまったく影響しません。空のコレクションへの参照を保持します。

私の推測では、ボタンクリックイベントでlegend.xaml.cs内で「正常に機能する」次のようなコードを使用していると思います。

itmCtrol.ItemsSource = allLines;

これにより、itmCtrol.ItemsSource参照が空のコレクションから正しく入力されたコレクションにスワップされます。

最も簡単な解決策は、allLinesフィールドを、itmCtrol.ItemSourceプロパティに直接委任するプロパティに置き換えることです(新しいプロパティのバッキングフィールドとしてitmCtrol.ItemsSourceプロパティを使用します)。こんな感じになります。

    public ObservableCollection<StringLineInfo> AllLines
    {
        get
        {
            return (IObservableCollection<StringLineInfo>)itmCtrol.ItemsSource;
        }
        set
        {
            itmCtrol.ItemsSource = value;
        }
    }
于 2011-07-19T13:54:16.413 に答える