我有一组仪器,在我将它们添加到列表后,我想配置它们
目前,这个工具被定义为一个继承自ObservableObject的类,并存储在一个ObservableCollection中,显示为一个包含仪器详细信息的数据板的列表视图。
现在我可以添加/删除一种仪器了。但当我试图更新仪器的细节(如名称,类型)。它不是更新到仪器类属性。
我使用的是CommunityToolkit.Mvvm,我测试了如果我只是将一个工具细节放在文本框中作为主窗口中的直接元素,而不是像listviewitem那样工作的话,它就能工作。那么,这是否意味着我不能将observableObject置于另一个可观察的another /ObserverbaleCollection之下?
<ListView x:Name="ListView_Instr"
ItemsSource="{Binding InstrumentConfigs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectInstrumentConfig, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Padding="5,5,5,5" Grid.Row="0">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="{Binding Width, ElementName=ListView_Instr}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="CheckBox_InstrChecked" Grid.Column="0"/>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<Label Content="Type"/>
<ComboBox x:Name="ComboBox_InstrType" ItemsSource="{Binding InstrTypes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="2">
<Label Content="Name"/>
<TextBox x:Name="TextBox_InstrName" Text="{Binding InstrName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="3">
<Label Content="Addr"/>
<TextBox x:Name="TextBox_InstrAddr" Text="{Binding InstrAddr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="4">
<Label Content="Interface"/>
<ComboBox x:Name="ComboBox_InstrInterface" ItemsSource="{Binding InstrInterfaceTypes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> 在使用可观察的时候,我遗漏了什么细节?
谢谢
发布于 2022-11-04 09:59:20
下面的方法应该有效。
Instrument.cs
using CommunityToolkit.Mvvm.ComponentModel;
namespace wpf_binding
{
[INotifyPropertyChanged]
public partial class Instrument
{
[ObservableProperty]
private string name;
[ObservableProperty]
private string description;
partial void OnNameChanged(string value)
{
System.Console.WriteLine($"Name changed: {Name}");
}
partial void OnDescriptionChanged(string value)
{
System.Console.WriteLine($"Description changed: {Description}");
}
}
}ViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
namespace wpf_binding
{
[INotifyPropertyChanged]
public partial class ViewModel
{
[ObservableProperty]
private ObservableCollection<Instrument> instruments;
[ObservableProperty]
private Instrument selectedItem;
public ViewModel()
{
Instruments = new ObservableCollection<Instrument>()
{
new Instrument() { Name = "Item 1", Description = "Desc 1" },
new Instrument() { Name = "Item 2", Description = "Desc 2" }
};
}
}
}MainWindow.xaml
<Window x:Class="wpf_binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf_binding"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical">
<ListView ItemsSource="{Binding Instruments}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Name, Mode=TwoWay}"/>
<TextBox Text="{Binding Description, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:"/>
<TextBlock Text="{Binding SelectedItem.Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Description:"/>
<TextBlock Text="{Binding SelectedItem.Description}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Window>MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf_binding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
}https://stackoverflow.com/questions/74314711
复制相似问题