在RoomViewModel和OnPropertyChanged(nameof(Room))中更改Room时;但标签绑定房间.NameRoom不更新。数据室已更改。
我不知道如何解决这个问题。有人能帮我吗?
在先进的,非常感谢。
RoomViewModel
using Learning.Core;
using Learning.EventRegister;
using Learning.MVVM.Model;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Learning.MVVM.ViewModel
{
public class RoomViewModel : ObservableObject
{
public List<Item> items { get; }
private Room room;
private string nameRoom;
public RoomViewModel()
{
room = new Room("1", false);
nameRoom = room.NameRoom;
items = new List<Item>();
}
public Room Room
{
get => room; set
{
room = value;
OnPropertyChanged(nameof(Room));
}
}
}
}
RoomView.xaml**<Label Content="{Binding Room.NameRoom}"**
<UserControl x:Class="Learning.MVVM.View.RoomView"
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:local="clr-namespace:Learning.MVVM.View"
xmlns:viewModel="clr-namespace:Learning.MVVM.ViewModel"
mc:Ignorable="d">
<UserControl.DataContext>
<viewModel:RoomViewModel/>
</UserControl.DataContext>
<StackPanel>
<Label Content="{Binding Room.NameRoom}"
HorizontalAlignment="Center"
Foreground="White"/>
<Border Background="#cbc531"
Padding="10"
Margin="10"
CornerRadius="10">
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Giờ đến: "
FontWeight="Bold"/>
<Label Content="12:29 AM"
Grid.Column="1"/>
<Label Content="Thời gian hát: "
Grid.Row="1"
FontWeight="Bold"/>
<Label Content="10 phút"
Grid.Row="1"
Grid.Column="1"/>
<Button Background="Transparent"
BorderBrush="Transparent"
Padding="0"
Width="50"
Height="50"
Grid.RowSpan="2"
Grid.Column="2">
<Image Source="../../Images/invoice.png"/>
</Button>
</Grid>
</StackPanel>
</Border>
<ListView
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Center"
BorderBrush="Transparent"
ItemsSource="{Binding items}"
ItemContainerStyle="{StaticResource MenuItemInRoom}"
>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Transparent" Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<Border Background="#cbc531"
Padding="10"
Margin="10"
CornerRadius="10">
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Tạm tính: "
FontWeight="Bold"/>
<Label Content="200000"
Grid.Column="1"/>
<Label Content="Khoản thu khác: "
Grid.Row="1"
FontWeight="Bold"/>
<Label Content="10000"
Grid.Row="1"
Grid.Column="1"/>
<Label Content="Tổng tiền: "
Grid.Row="2"
FontWeight="Bold"/>
<Label Content="210000"
Grid.Row="2"
Grid.Column="1"/>
<Button Background="Transparent"
BorderBrush="Transparent"
Padding="0"
Width="50"
Height="50"
Grid.RowSpan="3"
Grid.Column="2">
<Image Source="../../Images/invoice.png"/>
</Button>
</Grid>
</StackPanel>
</Border>
</StackPanel>
</UserControl>
Room.cs
using Learning.Core;
using Learning.MVVM.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Learning.MVVM.Model
{
public class Room : ObservableObject
{
private string name;
private bool isActive;
public Room(string name, bool isActive) {
this.name = name;
this.isActive = isActive;
}
public string Name
{
get => name;
set
{
name = value;
OnPropertyChanged(nameof(NameRoom));
}
}
public bool IsActive { get => isActive; set
{
isActive = value;
OnPropertyChanged(nameof(ColorRoom));
}
}
public string ColorRoom
{
get
{
if (isActive )
{
return "#ffce00";
}else
{
return "White";
}
}
}
public string NameRoom
{
get
{
return $"Phòng {name} - Lầu 1";
}
}
public object This
{
get => this;
}
}
}
主视图模型
using Learning.Core;
using Learning.EventRegister;
using Learning.MVVM.Model;
using Prism.Commands;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Learning.MVVM.ViewModel
{
public class MainViewModel : ObservableObject
{
public RelayCommand HomeViewConmmand { get; set; }
public RelayCommand DiscoveryViewConmmand { get; set; }
public HomeViewModel HomeVM { get; set; }
public DiscoveryViewModel DiscoveryVM { get; set; }
public RoomViewModel RoomVM { get; set; }
private object _currentView;
private object _rightView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
public object RightView
{
get { return _rightView; }
set
{
_rightView = value;
OnPropertyChanged();
}
}
EventAggregator _eventAggregator;
public MainViewModel()
{
_eventAggregator = new EventAggregator();
HomeVM = new HomeViewModel(_eventAggregator);
DiscoveryVM = new DiscoveryViewModel();
RoomVM = new RoomViewModel();
_eventAggregator.GetEvent<CommandSendEvent>().Subscribe(GetRoom);
CurrentView = HomeVM;
RightView = RoomVM;
HomeViewConmmand = new RelayCommand(o =>
{
CurrentView = HomeVM;
});
DiscoveryViewConmmand = new RelayCommand(o =>
{
CurrentView = DiscoveryVM;
});
}
private void GetRoom(Room cur_room)
{
RoomVM.Room = cur_room;
}
}
}
1条答案
按热度按时间2admgd591#
从
RoomView
中删除它,因为您正在创建一个(MainViewModel
中的RoomViewModel
:设置在
MainViewModel
中创建的RoomViewModel
的Room
属性不会影响在RoomView
的标记中创建的其他示例。应该只有一个RoomViewModel
示例。