我有一个静态ObservableCollection>在非静态类ColorManager(单例)中命名为HeaderColorPairs。
此属性绑定到ListView的ItemsSource,如下所示:
<ListView.ItemsSource>
<MultiBinding Converter="{StaticResource xmlFormatter}" ConverterParameter="ColorList">
<Binding Source="{StaticResource ResourceKey=colorManager}" Path="HeaderColorPairs"></Binding>
<Binding Path="LogEntryListViewModel.SelectedLogEntry.Model.RawXml"></Binding>
</MultiBinding>
</ListView.ItemsSource>
这就是转换方法:
public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
{
//parameter = outputtype
parameter = (parameter == null) ? "" : parameter;
string rawXml = value[1].ToString();
if (rawXml != DependencyProperty.UnsetValue.ToString() && rawXml != string.Empty)
{
OutputTypes opt = (OutputTypes)Enum.Parse(typeof(OutputTypes), parameter.ToString().Length.ToString());
if (opt == OutputTypes.Xml)
{
RawXmlFormatter rxf = new RawXmlFormatter(rawXml);
return rxf.XmlStringArrayToRunList();
}
else if (opt == OutputTypes.ColorList)
{
return ColorManager.GetColorsRelatedToXml(rawXml);
}
}
return null;
}
public static ObservableCollection<KeyValuePair<string, Color>> GetColorsRelatedToXml(string rawXml)
{
List<string> headerList = RawXmlFormatter.GetHeadersFromRawXml(rawXml);
SetUnsetColors(headerList);
return new ObservableCollection<KeyValuePair<string, Color>>(HeaderColorPairs.Where(kvp => headerList.Contains(kvp.Key)));
}
绑定工作得很好,它显示了ObservableCollection权限,但如果我在代码隐藏中更改集合,则ListView不会更新。
答案 0 :(得分:0)
我现在使用的是非静态属性,而不是单例模式(也可以使用)。问题解决了;)