我写了一个程序,它读取XML数据并将这些数据存储在一个可观察列表中。现在我有两个可观察的字符串数组列表,我想在一个有两列的列表视图中显示这两个不同的数组。
这是我的xaml:
<views:MvxWpfView
x:Class="Ui.MVVMMaPa.Views.HomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
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:Ui.MVVMMaPa.Views" xmlns:core="clr-namespace:Ui.MVVMMaPa.Core" d:DataContext="{d:DesignInstance Type=core:HomeViewModel}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel>
<TextBox Text="{Binding Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Browse" Command="{Binding BrowseCommand}" />
<ListBox Name="ListBox">
<TextBlock Name="TextBlockName" Text="{Binding ShortNameStringObservableList}" />
<TextBlock Name="TextBlockid" Text="{Binding ValueStringObservableList}"/>
</ListBox>
</StackPanel>
</Grid>
</views:MvxWpfView>
字符串
我的视图模型:
using Logic.MVVMMaPa.ListCreater;
using MvvmCross.Commands;
using MvvmCross.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Ui.MVVMMaPa.Core
{
public class HomeViewModel : MvxViewModel
{
public DirectoryIterator directoryIterator = new DirectoryIterator();
private List<MachineparameterClass> machineparameterList = new List<MachineparameterClass>();
public IMvxCommand BrowseCommand => new MvxCommand(Browse);
private ObservableCollection<string[]> shortNameStringObservableList = new ObservableCollection<string[]>();
public ObservableCollection<string[]> ShortNameStringObservableList
{
get { return shortNameStringObservableList; }
set
{
shortNameStringObservableList = value;
RaisePropertyChanged(() => nameof(ShortNameStringObservableList));
}
}
private ObservableCollection<string[]> valueStringObservableList = new ObservableCollection<string[]>();
public ObservableCollection<string[]> ValueStringObservableList
{
get => valueStringObservableList;
set
{
valueStringObservableList = value;
RaisePropertyChanged(() => nameof(ValueStringObservableList));
}
}
private void Browse()
{
Comparer comparer = new Comparer(directoryIterator.FileIterationOne(directoryIterator.DirectoryIteratorFunction()));
machineparameterList = comparer.ListComparer();
for (int i = 0; i < machineparameterList.Count; i++)
{
ShortNameStringObservableList.Add(machineparameterList[i].getShortNames());
ValueStringObservableList.Add(machineparameterList[i].getValues());
}
}
}
}
型
这是我的模型:
using System.ComponentModel;
namespace Logic.MVVMMaPa.ListCreater
{
public class MachineparameterClass : INotifyPropertyChanged
{
string pluginName;
string pluginInstanceName;
string[] shortNames;
string[] values;
string[] typesInInt;
public event PropertyChangedEventHandler? PropertyChanged;
void OnPropertyChanged(string name) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public string PluginName
{
get { return pluginName; }
set { pluginName = value; }
}
public string PluginInstanceName
{
get { return pluginInstanceName; }
set { pluginInstanceName = value; OnPropertyChanged(nameof(PluginInstanceName)); }
}
public string[] ShortNames
{
get { return shortNames; }
set { shortNames = value; OnPropertyChanged(nameof(ShortNames)); }
}
public string[] Values
{
get { return values; }
set { values = value; OnPropertyChanged(nameof(Values)); }
}
public string[] TypesInInt
{
get { return typesInInt; }
set { typesInInt = value; OnPropertyChanged(nameof(TypesInInt)); }
}
public MachineparameterClass()
{
}
public MachineparameterClass(MachineparameterClass machineparameterClass)
{
this.pluginName = pluginName;
this.pluginInstanceName = pluginInstanceName;
this.shortNames = shortNames;
this.values = values;
this.typesInInt = typesInInt;
}
public void SetProperty(string prop, string val)
{
}
public MachineparameterClass(string pluginName, string pluginInstanceName, string[] shortNames, string[] values, string[] typesInInt) : base()
{
this.pluginName = pluginName;
this.pluginInstanceName = pluginInstanceName;
this.shortNames = shortNames;
this.values = values;
this.typesInInt = typesInInt;
}
public String getPluginName() { return this.PluginName; }
public String getPluginInstanceName() { return this.PluginInstanceName; }
public String[] getShortNames() { return this.ShortNames; }
public String[] getValues() { return this.Values; }
public String[] getTypes() { return this.typesInInt; }
}
}
型
首先,我通过调用列表视图的Add()方法添加了两个不同的列表,但没有绑定,这可以工作,但我不能正确地处理列表条目。例如,我想选择一些值并计算它们的平均值。然后我试图实现MVVM-Pattern,但我不能正确地绑定字段,所以如果observableList条目接收到条目,视图就会更新。
1条答案
按热度按时间qoefvg9y1#
您正在尝试将每个
TextBlock.Text
属性绑定到整个ObservableCollection
字符串。字符串
TextBlock.Text
想要一个字符串,而不是它们的集合。如果列表要显示“machines”,则创建一个类(视图模型)来表示每个列表条目 * 完整 *;暴露其中一台机器的所有属性。因此,不要为机器的各个元素创建单独的
ObservableCollection
s。创建一个新视图模型的ObservableCollection
,并将ListBox
绑定到 that。举例来说:
型
揭露一个可观察到的这些
型
然后将ListBox绑定到它。使用ItemTemplate来显示您需要查看的一台机器的任何内容。
型