【发布时间】:2021-02-01 12:34:01
【问题描述】:
INotifyPropertyChanged 已实现,DataContext 已设置。
这是我的树视图:
<TreeView ItemContainerStyle="{StaticResource MaterialDesignTreeViewItemExtended}" x:Name="TestCasesTree" MinWidth="220" ItemsSource="{Binding TestCases.Children, Mode=TwoWay}">
<i:Interaction.Behaviors>
<behaviours:BindableSelectedItemBehavior SelectedItem="{Binding SelectedTreeViewItem, Mode=TwoWay}" />
</i:Interaction.Behaviors>
<TreeView.Resources>
<con:TestAssessmentToImagePathConverter x:Key="TestAssessmentConverter" />
</TreeView.Resources>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children, Mode=TwoWay}" DataType="{x:Type data:TestCaseNode}">
<StackPanel Margin="0" Orientation="Horizontal">
<Image Source="{Binding TestAssessment, Converter={StaticResource TestAssessmentConverter}}" RenderOptions.BitmapScalingMode="HighQuality" Width="20" Height="20"/>
<TextBlock Margin="8 2 0 0" Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
绑定:
ItemsSource="{Binding TestCases.Children, Mode=TwoWay}"
BindableBase 类:
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected bool CanExecute
{
get
{
return true;
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
属性:
public TestCaseNode TestCases
{
get { return mainWindowModel.TestCases; }
set
{
mainWindowModel.TestCases = value;
SetProperty(ref _testCases, value);
}
}
孩子们:
public ObservableCollection<TestCaseNode> Children { get; set; }
我编辑东西的地方:
SelectedTreeViewItem.SetTestAssessmentForCluster(testAssessment);
OnPropertyChanged("Children");
OnPropertyChanged("TestCases");
OnPropertyChanged(nameof(TestCases));
OnPropertyChanged(nameof(TestCases.Children));
View 中没有任何变化(但是当我在 Visual Studio 中调试它时,我清楚地看到对象发生了变化)
这可行,但它会导致其他事情崩溃:
SelectedTreeViewItem.SetTestAssessmentForCluster(testAssessment);
var tc = TestCases;
TestCases = null;
TestCases = tc;
更新:
TestCaseNode 类:
public class TestCaseNode : TestCase, IEquatable<TestCaseNode>, IComparable<TestCaseNode>
{
public TestCaseNode Parent { get; set; }
public ObservableCollection<TestCaseNode> Children { get; set; }
public void Add(TestCaseNode node) {...}
public void SetTestAssessmentForCluster(TestAssessment testAssessment, ref TestScores _testScores) {...}
public bool Equals(TestCaseNode other) {...}
public override bool Equals(object obj) {...}
public override int GetHashCode() {...}
public int CompareTo(TestCaseNode other) {...}
}
TestCase 类:
public class TestCase
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; }
public string Description { get; set; }
public ObservableCollection<TestStep> TestSteps { get; set; }
public TestAssessment TestAssessment { get; set; } = TestAssessment.EMPTY;
}
【问题讨论】:
-
我认为
OnPropertyChanged("Children");需要从TestCaseNode类中调用 - 这就是ObservableCollection<TestCaseNode> Children所在的位置 - 是吗? -
所以我需要从 TestCaseNode 派生 BindableBase 类?好的,我试试看。
-
所以我在 TestCaseNode 中派生了 BindableBase 并将
OnPropertyChanged("Children")放在SetTestAssessmentForCluster方法的末尾 - 它不起作用。 -
不是从 BindableBase 派生的。包含
ObservableCollection<TestCaseNode> Children' must include theOnPropertyChanged("Children");` 调用的对象。也就是说,OnPropertyChanged("Children");调用必须由TestCases类发出。请调整您的问题以包含更多此类内容。