私はこの質問がたくさん尋ねられていることは知っていますが、私はまだこれを得ることができません。バインドしようとしているオブジェクトはコードの中で正しく更新されているため、動作していない唯一のものは、ItemsSourceが変更されたときに更新されるTreeViewItemの子です。TreeViewItemがバインドされたItemsSource、ObservableCollection、およびINotifyPropertyChangedで更新されない
私はすべてが正しく設定されているようですが、おそらく、これを機能させないように物事をまとめている方法について何かがあります。私はC#.NET 4.5 WPFプロジェクトをVS 2015 Windows 7で使用しています。静的クラスの静的プロパティにバインドして、TreeViewItemのItemsSourceとDisplayMemberPathを設定するメソッドしかありません。
XAML:
<!-- Menu tree -->
<TreeView Grid.Column="0"
x:Name="menutree"
Background="Transparent"
BorderThickness="0">
<!-- Profiles TVI -->
<TreeViewItem Header="{x:Static loc:Resources.profiles}"
IsExpanded="True">
<!-- Color profile TVI -->
<TreeViewItem x:Name="colorTvi"
Header="{x:Static loc:Resources.colorProfiles}"
MouseRightButtonDown="colorTvi_MouseRightButtonDown"
DisplayMemberPath="Name"
ItemsSource="{Binding Source={x:Static local:Shared.ColorProfiles}, Mode=OneWay}" />
<TreeViewItem ...
クラス/プロパティにバインドされている:
public static class Shared
{
#region Getter/Setter
// Notify property changed
public static NotifyChanged Notify { get; set; } = new NotifyChanged();
// All profiles that have been created
public static List<Profile> Profiles
{
get { return _Profiles; }
set
{
// Set profile
_Profiles = value;
Notify.OnPropertyChanged(nameof(Profiles));
Notify.OnPropertyChanged(nameof(ColorProfiles));
}
}
private static List<Profile> _Profiles = new List<Profile>();
// Color profiles
public static ObservableCollection<ColorProfile> ColorProfiles
{
get
{
return new ObservableCollection<ColorProfile>(
Profiles?.Where(m => m.GetType() == typeof(ColorProfile))?.Cast<ColorProfile>()?.ToList() ??
new List<ColorProfile>());
}
}
#endregion
}
NotifyChangedクラス:
// Property changed class
public class NotifyChanged : INotifyPropertyChanged
{
// Property changed event
public event PropertyChangedEventHandler PropertyChanged;
// Notify property changed
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
が、私はこれを呼び出すことなく、動作するように取得したいと思いコードの後ろにRefresh()。私はShared.Profilesに直接バインドしようとしましたが、それは何の助けにもなりません。 ColorProfileは、Profileから継承する基本クラスです。
愚かでシンプルなものがあります。助けを前にありがとう。
UPDATE:それは実際のItemsSourceのように見え、さらに検査で
も更新していないです。デバッグ中、ItemsSourceがObservableCollectionにバインドされているが、ItemsSourceがリストに加えられた変更を反映していないことを、コントロールのプロパティエクスプローラで確認できます。私が手作業でコードをバインドしている場合、それは機能します。
通常、このような問題は、repaintイベントが呼び出されないためです。単純な解決策は、単にItemSourceをnullに設定してから実際のソースに戻すことです。 – jdweng
私はそれをすることができます。それは私がいつも過去に働いていた方法ですが、余分なコードなしでこれを稼働させるのが良いでしょう。 – brandonstrong