カスタムクラスを含むユーザーコントロールで作成された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」関数を呼び出すだけです。
起動時にそれらを等しく設定できない理由について何かアイデアはありますか?