【发布时间】:2022-01-23 21:23:09
【问题描述】:
我是 WPF 新手,在尝试将多个 TextBlocks 绑定到我的“ItemViewModel”-List 的属性时遇到了问题。
HomeView.xaml 是一个用户控件,显示在主窗口上。 我在所有数据所在的位置创建了自己的 HomeViewModel.cs,因此我不使用 HomeView.xaml.cs,也没有更改该代码中的任何内容。
我的目标是,每当“HomeViewModel.cs”中的“_items[i]._weight”或“_items[i]._count”的值发生变化时,“HomeView.xaml”中的绑定 TextBlock 或 TextBox将获得新值。
另外,例如,我如何告诉“item1WeightTBx”它绑定到“_items[0]._weight”而不是“_items[3]._weight “?
到目前为止,“HomeView.xaml”一直在“HomeView”而不是“HomeViewModel”中寻找数据。
如果您需要更多代码或信息,请告诉我 :)
提前谢谢你!
HomeView.xaml(用户控制)
<UserControl x:Class="Project.MVVM.View.HomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModel="clr-namespace:Project.MVVM.ViewModel"
xmlns:local="clr-namespace:Project.MVVM.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="109*"/>
<RowDefinition Height="156*"/>
<RowDefinition Height="185*"/>
</Grid.RowDefinitions>
<TextBox x:Name="item1WeightTBx"
Grid.Row="2"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
DataContext="{Binding Items}"
Text="{Binding _weight}"
Height="23"
Margin="51,39,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="40"
FontSize="14"/>
<TextBlock x:Name="item1CountTBk"
HorizontalAlignment="Left"
Grid.Row="2"
Height="28"
Margin="51,65,0,0"
TextWrapping="Wrap"
DataContext="{Binding Items}"
Text="{Binding _count}"
VerticalAlignment="Top"
Width="57"
FontSize="16"
Foreground="#424242"/>
<TextBox x:Name="item2WeightTBx"
Grid.Row="2"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
DataContext="{Binding Items}"
Text="{Binding _weight}"
Height="23"
Margin="123,39,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="40"
FontSize="14"/>
<TextBlock x:Name="item2CountTBk"
HorizontalAlignment="Left"
Grid.Row="2"
Height="28"
Margin="123,65,0,0"
TextWrapping="Wrap"
DataContext="{Binding Items}"
Text="{Binding _count}"
VerticalAlignment="Top"
Width="57"
FontSize="16"
Foreground="#424242"/>
.
.
.
HomeViewModel.cs
public List<ItemViewModel> _items;
public List<ItemViewModel> Items => _items;
public HomeViewModel(Connection connectionInterface, ShelfComp shelfComp)
{
_connectioninterface = connectionInterface;
_scale = new Scale("0,0;0,0", ShelfWidth);
_shelfComp = shelfComp;
for (int i = 0; i < shelfComp.ItemList.Count; i++)
{
_items.Add(new ItemViewModel(_shelfComp.ItemList[i]));
}
}
ItemViewModel.cs
class ItemViewModel : ObservableObject
{
public Item _item;
public string _name => _item.Name;
public int _minIndex => _item.MinIndex;
public int _maxIndex => _item.MaxIndex;
public double _weight => _item.Weight;
public int Width => _item.Width;
public int _count => _item.Count;
public ItemViewModel(Item item)
{
_item = item;
}
}
Item.cs
public class Item
{
public string Name { get; set; }
public int MinIndex { get; set; }
public int MaxIndex { get; set; }
public double Weight { get; }
public int Width { get; }
public int Count { get; set; }
public Item(string Name, int MinIndex, int Width, double Weight)
{
this.Name = Name;
this.MinIndex = MinIndex;
this.Width = Width;
this.MaxIndex = MinIndex + Width;
this.Weight = Weight;
Count = 0;
}
}
ObservableObject.cs
class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
-
您应该使用带有支持字段的完整属性,并且每次设置属性值时,您都需要调用
OnPropertyChanged()。 -
顺便不用重新发明轮子,看看
Microsoft.Toolkit.Mvvm库:docs.microsoft.com/en-us/windows/communitytoolkit/mvvm/…
标签: c# wpf xaml data-binding binding