我treeView在 XAML 中定義為:
<UserControl.Resources>
<models:TreeLines x:Key="myLines" x:Name="myLinesData"/>
</UserControl.Resources>
<TreeView x:Name="treeData"
Grid.Column="1" Background="#282828" BorderThickness="0" Padding="0,5,0,0"
SelectedValuePath="Uid">
<TreeViewItem x:Name="tLines"
ItemsSource="{Binding Source={StaticResource myLines}, Path=MyLines}"
Style="{StaticResource custTVItem}" Header="Lines" Uid="tabLines">
<TreeViewItem.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:Lines}"
ItemsSource="{Binding lineSet}">
<TextBlock Text="{Binding productName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:LineSets}"
ItemsSource="{Binding lineName}">
<TextBlock Text="{Binding setName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:LineNames}"
ItemsSource="{Binding dataTypes}">
<TextBlock Text="{Binding lineName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:LineData}"
ItemsSource="{Binding dataVals}">
<TextBlock Text="{Binding dataType}"/>
</HierarchicalDataTemplate>
</TreeViewItem.Resources>
</TreeViewItem>
</TreeView>
UserControl.Resources指向一個類:
public partial class TreeLines : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(MainWindow.treeData.ItemsSource))]
private List<Lines>? myLines;
}
我在這里得到的錯誤是:
[NotifyPropertyChangedFor] 的目標必須是(不同的)可訪問屬性
myLines我試圖系結到的物件有后面的類,如TreeView`HierarchicalDataTemplates 所示:
public class Lines
{
public string productName { get; set; }
public List<LineSets> lineSet { get; set; }
}
public class LineSets
{
public string setName { get; set; }
public List<LineNames> lineName { get; set; }
}
public class LineNames
{
public string lineName { get; set; }
public List<LineData> dataTypes { get; set; }
}
public class LineData
{
public string dataType { get; set; }
public List<double> dataVals { get; set; }
}
如果我洗掉所有 CommunityToolkit.MVVM 方面并設定我的變數:
private List<Lines>? myLines;手動通過將其更改為 public 并在加載時為其分配資料,那么它僅在加載時填充。
我需要myLines在我的 C# 代碼中即時修改,而這又應該更新 treeView。您可以看到我正在嘗試通過資料系結自動實作這一點,但有些事情是不對的。
我認為錯誤可能在于:
[NotifyPropertyChangedFor(nameof(MainWindow.treeData.ItemsSource))]
和/或可能StaticResource在 XAML 中的用法:
<TreeViewItem x:Name="tLines"
ItemsSource="{Binding Source={StaticResource myLines}, Path=linesCollection}"
Style="{StaticResource custTVItem}" Header="Lines" Uid="tabLines">
請告知您是否可以提供幫助
uj5u.com熱心網友回復:
用 替換所有List<T>屬性ObservableCollection<T>。然后,每當您從這些集合中添加或洗掉專案時,視圖都會更新。
為了使視圖在您更改集合中單個專案的屬性時也更新,您更改的屬性的類應該實作INotifyPropertyChanged介面并引發更改通知。
Lines這是您應該如何實作該類的示例:
public class Lines : ObservableObject
{
[ObservableProperty]
private string productName { get; set; }
[ObservableProperty]
private ObservableCollection<LineSets> lineSet { get; set; }
}
系結到生成的屬性(以大寫字母開頭):
<HierarchicalDataTemplate DataType="{x:Type models:Lines}"
ItemsSource="{Binding LineSet}">
<TextBlock Text="{Binding ProductName}"/>
</HierarchicalDataTemplate>
uj5u.com熱心網友回復:
[NotifyPropertyChangedFor(nameof(MainWindow.treeData.ItemsSource))]不需要添加。
無需實施額外的通知。因為[ObservableProperty]已經在實作通知功能了。
查看自動生成的來源。
[NotifyPropertyChangedFor(parameter)]的引數應該是類內屬性的名稱。
public partial class TreeLines : ObservableObject
{
[ObservableProperty]
private List<Lines>? myLines;
public string OtherProperty1 { get; set; }
public string OtherProperty2 { get; set; }
}
在這種情況下,[NotifyPropertyChangedFor]的可能引數只有 MyLines、OtherProperty1 和 OtherProperty2。
[NotifyPropertyChangedFor]是一個屬性,表示類內連接的其他屬性發生了變化
這是一個例子。
public partial class GetSum : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Sum))]
private int num1;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Sum))]
private int num2;
public int Sum { get => num1 num2; }
}
在呼叫 Num1 屬性的 setter 時,同時更新螢屏系結的 Num1 值和 Sum 值。
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
Height="450" Width="800">
<Window.DataContext>
<local:GetSum/>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding Num1}"/>
<TextBox Text="{Binding Num2}"/>
<TextBlock Text="{Binding Sum}"/>
</StackPanel>
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/504347.html
